Types are JWT/token based authentication and cookie-based authentication.

When we sign in to an app/site i.e. give my creds, press “sign-in”, POST request goes to /signin endpoint on the server, it extracts my username and password, checks and validates if the creds are present in database, if yes, i am signed in and the server returns me a token (need to keep safe) as this token will help me to identify myself to the particular server in the future. This token gets stored in my browser.

Now in all subsequent requests to the server (GET/POST/…), I am sending the above token along with the request which lets the server know that its me…this token is unique to me. This token allows persistent sessions as I am only logging in once and using this token, I can access anything on the app until I log out. Auth workflow below:

image.png

Although 2 people send request to the same endpoint GET/ courses, they get back different data i.e. only the courses they bought how??

because the token is something like : { user_id : 123,ttl : 12} for one user and for other {user_id : 345,ttl:11}

so when they access same route, the server extracts the user_id from the token present in the incoming request and checks if that user-id present in dB …if yes, sends me back only the courses associated to that user id i.e. dB[123] will have different courses than dB[456].

So, after the user successfully logged in and we send him the token, we create authenticated endpoints (all the endpoints which will expect the earlier sent token in the request body).

The problem is that we need to send a request to the database every time the user wants to hit an authenticated endpoint when using basic tokens. Hence. we use JWT’s.

Normal tokens are stateful tokens i.e. need to be stored in a database….unlike JWT’s.

JWT’s can be decoded by anyone to get the username/email but verification can only be done using JWT_SECRET. Verification is different from decoding as former confirms that the email/username belongs to me whereas latter just gives me back the associated email with the JWT.

FE→BE workflow :