How Are JWTs Used to Authenticate Angular 4 Applications?

How Are JWTs Used to Authenticate Angular 4 Applications?

In Annular, the following Steps are used to building authentication and authorization for RESTful APIs and applications. It might help you -
1.     The users send their credentials to the server which is verified by the database credentials. If everything is verified successfully, the JWT is sent back to them.
2.     The JWT is saved in the user's browser in local storage or in a cookie and so on.
3.     The presence of a JWT saved in the browser is used as an indicator that a user is currently logged in.
4.     The expiry time of JWT is continually checked to maintain an authenticated state in the Angular applications.
5.     The client side routes are protected and access by authenticated users only.
6.     When user sends the XHR requests for APIs, the JWT gets sent an Authorization header using your cookies or Bearer.
7.     When XHR requests coming on the server, before send back the responses it’s validated first with configured app's secret keys. If everything is looking good then returns successfully responses other send the back to the bad request.


There are several open source libraries are available for angular which are helps with JWTs and has the ability to Decode the JWT, Authorization header to XHR requests and so on.

What is JSON Web Token?
JSON Web Token (JWT) is an open standard which used for securely transmitting information between parties as a JSON object.

The JWTs can be signed with -
1.     HMAC algorithm
2.     RSA algorithm

When should you use JSON Web Tokens?
There are some scenarios where we can used JSON Web Tokens -
1.     Authentication
2.     Information Exchange

What is the JSON Web Token structure?
The JSON Web Tokens consist of three parts separated by dots (.), which are:
1.     Header
2.     Payload
3.     Signature

A JWT typically looks like –
######.######.######


Let's break down the different parts -
Header
{
  "alg": "HS256",
  "typ": "JWT"
}
//OR
{
  "alg": "RS256",
  "typ": "JWT"
}


Payload
{
  "sub": "1234567890",
  "name": "Anil Singh",
  "admin": true
}


Signature
HMACSHA256(base64UrlEncode(header) + "."base64UrlEncode(payload),  secret);





I hope you are enjoying with this post! Please share with you friends!! Thank you!!!
ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - https://code-sample.com and https://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions