0

In my project, I'm using the nodeJS and react. For the session on the backend side, I'm using the httpOnly cookie. So when the user login, we're sending the httpOnly cookie with JWT token (httpOnly cookie contains JWT token). Now using this httpOnly cookie, I'm checking the authentication on the backend side.

Now, the problem is how to manage the session (or do authentication check) on react side? because here, we can't access the httpOnly cookie using the javascript.

For example:

  • /login route --> httpOnly cookie send to the user
  • /dashboard ----> Suppose, the user wants to open it route without login then we first need to check whether the user is valid or not (If we use local storage then it would be easy because we only need to check the token store in local storage). Now how to check the authenticity of the user in the cookie?

It would be great help if anyone has any idea to resolve this problem.

1 Answer 1

2

If you want to use only httOnly cookies then your only option is to implement authentication check on your BE, which will return response based on that cookie (and you use this endpoint for checking authentication).

Other option would be not to use httpOnly cookies. So if you switch to regular cookies you will be able to verify JWT also on the FE like on BE (verifying signature of the JWT before you preform additional actions).

But you should know that normal cookies and also httpOnly cookies can be stolen, so this is the reason that they do not recommend to store JWT tokens in cookies. (but yes httpOnly cookies are a lot safer then regular, but still not 100% safe )

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @user2704821 for your time. I understood your point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.