In my nodejs app, I am using jwt tokens for authentication. Now after the token gets created , it is going to be verified. For that, it should be stored somewhere for verification purpose. So, my question is, should I store it in separate database schema against concerned userid or should I store it somewhere else?
JWTs do not need to be stored on the server side. When you create a JWT, you encrypt it using a secret - think of this as the "password." Then you send it to the client where it can be stored.
When the client makes a request, it sends the JWT along with it. On the server side, you can the decrypt it using the same secret. If the secret does not work, you know it is an invalid JWT.
For obvious reasons, your JWT secret should be kept secret! The best way to do this is to store it as an environment variable.
There isn't any need to store JWTs on the server side. That is the whole point as JWTs enable "asynchronous" token verification on the server side.
Here is a series of steps involved, putting in a simplified way:
1- Server creates a JWT using a secret (maintaining this secret across server nodes, if they are more than one, is important as the client can connect to any one of them servers with the given JWT)
2- Client stores this JWT and furnishes it in the subsequent requests to the server
3- The server (or any given server node in the farm) verifies the incoming JWT based on the secret that was used earlier to encrypt/create a JWT
HTH
© 2022 - 2024 — McMap. All rights reserved.