For JWT authentication, the RESTful API typically exposes an authentication endpoint, returning a token if the authentication is successful. When a user tries to log in, we can call the API and get the token:
executeJwtService(user, pwd) {
return axios.post('/authenticate', {
user,
pwd
})
}
We are making a POST request to the authentication URL with the user and password in the request body.
If the request is successful, we can take the token from the response and use it to set up axios interceptors to use the token with every subsequent RESTful API call:
this.setupAxiosInterceptors(this.setupJWTToken(token))
setupJWTToken(token) {
return 'Bearer ' + token
}
The algorithm for implementing authentication is simple. Capture the token at user login and use it with every subsequent RESTful API call.