What is JWT?
JWT is Stand for “JSON Web Token”. It is stateless Authentication in Angular Apps and Easily Angular apps are authenticated with JWT!
Stayed Informed – 125 Best Angular 2 Interview Questions and Answers
JWT Contain Three Parts -
1. Header – Is used to define the algorithm to sign the token and token type.
2. Payload – It is used to keep a JSON object of all the claims which you want.
3. Signature - It is secret key and used to verify the Signature!
JWT Key Features -
1. Decode a JWT from your Angular app and read its payload
2. Attach the JWT as an Authorization header to XHR requests
3. Existing service method (logging in and logging out) are used to checks whether the current user's JWT is expired or not.
JWT Installation from NPM -
Application Module Configuration -
AUTH_PROVIDERS default Configuration Options-
1. Header Name: Authorization
2. Header Prefix: Bearer
3. Token Name: token
4. Token Getter Function: (() => localStorage.getItem(tokenName))
5. Supress error and continue with regular HTTP request if no JWT is saved: false
6. Global Headers: none
Advanced Configuration -
1. AuthHttp - allows us to authenticated HTTP requests
2. tokenNotExpired - allows us to check expired or not JWT in local storage.
Example As -
How to set Per-Request Headers? How to Call Authenticated Requests?
Example As –