How to add login authentication and session to ReactJS React Router and Redux with MongoDB + NodeJS Express? How to add login authentication and session to ReactJS React Router and Redux with MongoDB + NodeJS Express? express express

How to add login authentication and session to ReactJS React Router and Redux with MongoDB + NodeJS Express?


i recommend you to use this library of JWT, it works perfectly, with expressJshttps://www.npmjs.com/package/jsonwebtoken,after your user sign in, you give theme a JWT token like this:

const token = jwt.sign(  { userId: user.userId, username: user.username },  yourFileWItchContainTheJwtString.jwtSecret,  { expiresIn: "1h" }//keep connected for 1 hour);

if you want to verify the JWT of the user to keep them connected or to give them some authorization you can verify by using :

jwtPayload = <any>jwt.verify(token, yourFileWItchContainTheJwtString.jwtSecret);//save it in locals res.locals.jwtPayload = jwtPayload;

//Get the jwt token from the head of request (do some action with the token)

const token = <string>req.headers["auth"];

you can check the documentation https://www.npmjs.com/package/jsonwebtoken


If i well understand you need to keep your user logged in. I would suggest to use jwt to create a token after your user is login jwt and store the this token into localstorage.setItem(token).

Hope it help