Home > Enterprise >  How to keep the user logged in after refreshing the page in Reactjs
How to keep the user logged in after refreshing the page in Reactjs

Time:10-27

I am working on a project and I've created a login page. I want to authenticate the user and keep the user logged in until he logs out. How do I do that using React, Express and MongoDB?

CodePudding user response:

You can use browser's localstorage and save the userInfo there and when he logs out you can clear the storage. You can check this set localstorage

Or you can make it from the server side with session-express and redis express sesson

CodePudding user response:

Proper basic implementation here is the followings:

  • backend authentication service that is checking credentials
    • this can be implemented in express and mongodb
  • the authentication service stores the user login status in session and cookies
  • on each render, the react app first ask express app to get user login status
    • if the user was logged in then show user dashboard
    • if the user wasn't logged in then show login page
  • Related