Hetarth7
Hetarth7

Reputation: 51

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?

Upvotes: 0

Views: 505

Answers (2)

pooyarm
pooyarm

Reputation: 26

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

Upvotes: 0

mustafa zakaria
mustafa zakaria

Reputation: 11

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

Upvotes: 1

Related Questions