-1

enter image description hereenter image description hereHow can I set the token in the local storage?now it is sent in the response body, should I use res.setheader?and if yes what to insert to it the token so that it will be set to local storage?

const token = jwt.sign({
    name: result[0].name
  },
  'somesupersecretsecret', {
    expiresIn: '1h'
  }
);
res.status(200).json({
token: token,
userId: "userid"
})

.catch(err => {
if (!err.statusCode) {
  err.statusCode = 500;
}
next(err);
});
}
});
roma
  • 1
  • 4
  • 1
    As you can see in the formatted code, there is an issue with brackets. Also you need to know the difference between server and client code – mplungjan Aug 31 '19 at 16:43
  • 1
    localStorage can be used on the client side. Node.js doesn't come with localStorage. Maybe you mean you would like to set a cookie? – George Aug 31 '19 at 16:43
  • i added an image of the frontend and the backend,the question is how shoud i send the token to the client so that it will be stored on its browser?should it be sent as the cookies like res.setheader or there is an option to store it as session or local storage?what is the write way – roma Aug 31 '19 at 17:04

1 Answers1

0

You can’t store any data in the browser’s local storage from your Node.js server-side (backend) code. You need to carry this data from backend to frontend JS code before you attempt to store the same in the browser’s local storage. Usually tokens are embedded into the headers.