Search code examples
reactjsmongodbexpresssessionmern

How to keep the user logged in after refreshing the page in Reactjs


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?


Solution

  • 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