Home > database >  How to design React signIn proccess with NodeJS and sessions stored in cookies?
How to design React signIn proccess with NodeJS and sessions stored in cookies?

Time:01-27

I have React signIn form and sessions mechanism implemented in NodeJs. In React I have protected routes only for authenticated users.

How should I check if user is authenticated. I have two ideas:

  1. If user sign in for the fisrt time I can save this information in LocalStorage and then evrytime just check localStorage.
  2. Send request to NodeJS server every time to check if user is authenticated.

Do you have any other ideas? Which solution should I pick?

I tried both options and second one is more UI unfriendly becasue I have to run loading state evrytime I am waiting for auth response. On the other hand, first option has also disadvantege, because I am based on token in LocalStorage which can be malicious.

CodePudding user response:

Every time a request is made to an endpoint that requires authentication, the request should contain proof that they are who they claim to be.

The classic way to do this is by storing some sort of "Session ID" in a cookie or localStorage (client side), that you send along with every request.

Using a "Token" (e.g: JWT) instead of a "Session ID" is another popular way to handle authentication.

Check out this article for more information about both: https://dzone.com/articles/cookies-vs-tokens-the-definitive-guide


To return to your question, I'm not sure what you're worried about in regards to a "malicious Token in localStorage" (Or do you mean localStorage can be malicious?). But for a secure application you have to either:

  • Store something client-side
  • Require your user to provide credentials (username password) for every request
  • Related