1. Authentication Mechanisms
To check whether the user is logged in or not we use different Authentication mechanisms
Commonly used Authentication mechanisms:
- Token Authentication
- Session Authentication
2. Token Authentication mechanism
We use the Access Token to verify whether the user is logged in or not
2.1 Access Token
Access Token is a set of characters which are used to identify a user
Example:
It is used to verify whether a user is Valid/Invalid
2.2 How Token Authentication works?
- Server generates token and certifies the client
- Client uses this token on every subsequent request
- Client don’t need to provide entire details every time
3. JWT
JSON Web Token is a standard used to create access tokens for an application This access token can also be called as JWT Token
3.1 How JWT works?
Client: Login with username and password Server: Returns a JWT Token Client: Sends JWT Token while requesting Server: Sends Response to the client
3.2 JWT Package
- jwt.sign()function takes payload, secret key, options as arguments and generates JWTToken out of it
- jwt.verify()verifies jwtToken and if it’s valid, returns payload. Else, it throws an error
4. Login User API by generating the JWT Token
When the user tries to log in, verify the Password. Returns JWT Token if the password matches else return Invalid Password with status code 400.
5. How to pass JWT Token?
We have to add an authorization header to our request and the JWT Token is passed as a Bearer token
6. Get Books API with Token Authentication
Here we check for JWT Token from Headers. If JWT Token is not present it returns an Invalid Access Token with status code 401 else verify the JWT Token.