VisheshRaju
VisheshRaju

Reputation: 348

How to set the token at client side in nodejs?

I have created a token using jwt in node js. But i am confused on how to store it in client side and send it back to server. I want to send the token in the header of every request to server.

How to add the token in local storage of client side ? I have not found an appropriate way to that also. Do i have to send the token in response headers and store it at local storage at client side ?? or is there any way to store at client local storage.Below is the code i use to generate token and send it in header.

var token = jwt.encode({
          iss: row[0].user_id,
          exp: expires
        }, 'jwtTokenSecret');
res.header('token', token);

Thanks !

Upvotes: 1

Views: 1205

Answers (1)

Oussama Romdhane
Oussama Romdhane

Reputation: 166

You'll have to return the token after login. To save it on the client side you don't have a lot of options; either you use cookies, local/session storage or web SQL databases.

I would personally recommend using sessionStorage (because of its volatility) I would also add a validation period to the token on the server side (if the user do not perform any action in a certain amount of time delete the token so the user might be disconnected), just keep in mind that that token can be seen by anyone when stored on the client side.

Also if you'll be using your API that way consider an API proxy and https

Upvotes: 1

Related Questions