Hope you are all doing well.
So I’m building MERN chat app, and i’m using httpOnly cookie
to store refresh jsonwebtoken
(it is also stored in the MongoDB).
Access token is stored in REDUX store.
I want to implement persistent userId
, but In socket.io private messaging documentation https://socket.io/get-started/private-messaging-part-2/, they are using sessions, and i kind of got lost there, as i’m not sure how to implement that with cookie.
Until now I was only using it to refresh access token.
Can i make persistent userId with cookie?
Do i need to do make sessions?
Or is there an easier way?
Generating cookie
const refreshToken = jwt.sign(
{ username: user.username },
process.env.REFRESH_TOKEN_SECRET,
{
expiresIn: "1d",
}
);
//Add refresh token to database
user.refToken = refreshToken;
user.save();
const userId = user._id
//Add refresh token to cookie
res.cookie("jwt", refreshToken, {
httpOnly: true,
secure: process.env.NODE_ENV !== "development",
sameSite: "strict",
maxAge: 24 * 60 * 60 * 1000,
});
Thanks in advance!!
Vladimir Sokorac is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.