I'm using the JWT to protect node js urls https://github.com/auth0/express-jwt
To create a JWT token user session i simply do:
-> auth/signup
-> jwt.sign(user_profile,secret,expireInMinutes:{900000000 /*almost never expires*/});
OR in case of login call
-> auth/login
-> jwt.sign(user_profile,secret,expireInMinutes:{900000000 /*almost never expires*/});
Every time a protected url is called i check for req.user
that is set up automatically by the JWT middleware.
Now I'm wondering:
1 - where does JWT tokens are stored when calling sign() ?
2 - do i have to verify() the token every time a protected url is called? if yes why?
3 - When i set a new token for an already signed user does the old token (if exists) gets deleted ? What if the expiration is not been set up or is 5 years for example?
4 - Why can't I set new tokens on same browser/app page ? I get invalid signature error if i register a new token but the token matches (i checked) It's like I can't signin more than 1 user on same browser
You need to store the token on the client side (local storage or cookie)
Yes. HTTP is stateless. If you don't verify it every time, someone could call your URL without the token or with an invalid token. If you are worried about performance, an HMACSHA256 check is very fast.
That doesn't make sense, you must be doing something wrong.
Sorry. this should be a comment on previous answer, but i don't have enough rep to comment so he it goes
@sbaang : Another reason to verify every time is thta there could be interesting "claims2 in the token, like allowing a user to access certain endpoints, not all of them. So in each verification you're not only verifying if the user is allowed to access the protected API, but that specific endpoint, based not on having a valid token but having a token that specifically allow it.
Yes. But "verify" is a little confusing term.
During token verification, no database check of user credentials is needed, because server have to trust received and verified (successfully decrypted) token. No server sessions storage is required to identify user.
You can think of JWT tokens like a simple session info, stored on client in an encrypted form. But if you need to cache more data in a user session info, I think, you still need some sort of sessions storage on a server, rendering JWT idea to almost useless compared to traditional Session ID in cookies.
You must have already figured out the answers to all your previous questions using the previous responses from the other users, but I will try to clear things up a bit for others too:
1 - where does JWT tokens are stored when calling sign() ?
2 - do i have to verify() the token everytime a protected url is called? if yes why?
3 - When i set a new token for an already signed user does the old token (if exists) gets deleted ? What if the expiration is not setted up or is 5 years for example?
4 - Why i can't set new tokens on same browser/app page ? I get invalid signature error if i register a new token but the token matches (i checked) It's like i can't signin more than 1 user on same browser