---
title: "Discussion on \"React, NodeJS and JWT Authentication - the right way!\" | Hashnode"
description: "Discussion on \"React, NodeJS and JWT Authentication - the right way!\". In this series of posts, we will create a secured end-to-end JWT-based authentication mechanism using NodeJS, Express, PassportJS and React. \nThere's a lot of information online about JWT-based authentication, however, I still see a lot of questions ..."
url: "https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [React, NodeJS and JWT Authentication - the right way!](https://blog.galmalachi.com/react-nodejs-and-jwt-authentication-the-right-way?utm_source=hashnode&utm_medium=feed)

In this series of posts, we will create a secured end-to-end JWT-based authentication mechanism using NodeJS, Express, PassportJS and React. There's a lot of information online about JWT-based authentication, however, I still see a lot of questions ...

Sep 3, 2020 · 14 min read · blog.galmalachi.com  
Updated Dec 21, 2020  
Read the article

-   [#jwt](https://hashnode.com/tag/jwt)
-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#typescript](https://hashnode.com/tag/typescript)
-   [#authentication](https://hashnode.com/tag/authentication)

## Written by

[Gal Malachi](https://hashnode.com/@galm)

@galm · Tel Aviv, Israel

  
[View profile](https://hashnode.com/@galm)

Developer

Fast learner, innovative, curious, highly motivated with a 'can-do' approach. I enjoy challenges and I thrive where there are technical questions that need to be answered.

## Responses 12

Markdown supported · ⌘↵ to post

[Wesley Oldaker](https://hashnode.com/@woldaker)

[Jun 16, 2024](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/666f48cfa28a7b998a572ed3)

First of all, the post is great, it has helped me a lot. I have it so very close to working. The encryption/decryption appears to be working fine, and it will set the refresh token in the cookie in the response, but that's as far as I can get.

I can't be 100% sure, but I think you may have some typos in the code you posted? I can't seem to get it to work as-is.

For example, in the function generateTokensAndAuthenticateUser(), you have await generateAccessToken(userId) immediately followed by a non-awaited generateRefreshToken(userId).

That doesn't make much sense to me. Are you completely sure that this code actually works when using the code you posted, verbatim?

Another one that I can't seem to get working is the step where you refresh the token by looking at req.cookies... That request object never has a property named 'cookies' for me. It does though, have the cookie set in the headers, under the name 'cookie', so const \[tokenName, tokenEncrypted\] = req.headers.cookie.split('=') gives me 'refresh\_token' and the gibberish contents of the token, respectively.

Another place that I had trouble with concerned the expirations of the tokens. It appears upon signing the token that you are using a value meant to mean expiresIn (a number of sec from now) and when that value appears again, it seems as if you're treating it as an absolute timestamp.

Oh, I should mention, I converted your ts into plain javascript to attempt this. So there's a possibility that I messed that up somehow, but still that doesn't explain the strange little discrepancies I see in the code as posted.

Thank you so much. I don't want to sound like I'm complaining. It looks like this took a lot of work to put together, and I've definitely learned a lot from it.

Btw, others have mentioned, do you have this on github anywhere? It would make this whole process 100x easier.

[Kavir Kaycee](https://hashnode.com/@kavir)

[Sep 12, 2023](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/6500ea2e1310dfd331e25ff0)

Well written [Gal Malachi](https://hashnode.com/@galm)

[TUSHAR JAISWAL](https://hashnode.com/@tushar2k)

[Sep 10, 2023](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/64fe130c703e32bdbc1ce975)

please provide a full code it more like we have to make a guess everytime we write line

[Himanshu Pareek](https://hashnode.com/@himanshu-pareek)

[Apr 2, 2021](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/606768997f07b6328b5dd9f3)

Hi [Gal Malachi](https://hashnode.com/@galm). Great blog. It is very useful and to the point article. Can you please share the source code? That would be great.

[Vadym Serdiuk](https://hashnode.com/@vadim24)

[Jan 4, 2021](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5ff39ac4e8e57e1374e681bc)

sorry could you give all the code? because it's not everything is clear, but I would like to figure out)

[Eliran Shem Tov](https://hashnode.com/@eliranshemtov)

[Oct 5, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f7b7beb9d088e603ef395b9)

Great post! Until now I used to save the access token in the local storage with expiration of 30 minutes or similar short term. On every full reload of the app or 5 min before access token's expiration - refresh using the refresh token. Is this in fact a bad practice? should I persist the access token in the session storage instead?

[Oct 5, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f7b7beb9d088e603ef395b9#reply-5f7b81ef9d088e603ef395d9)

Hi [Eliran Shem Tov](https://hashnode.com/@eliranshemtov),

Thanks for the feedback! Yes, it’s considered as a bad practice. You shouldn’t store the token in local/session storage. In part 2 of the series I deal with the client side, and provide code examples, I believe you’ll find some useful information there!

[Eliran Shem Tov](https://hashnode.com/@eliranshemtov)

[Oct 6, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f7b7beb9d088e603ef395b9#reply-5f7c12979d088e603ef39ae9)

[Gal Malachi](https://hashnode.com/@gal1419) wow! what a great lesson... thanks!

[Akash Raju M](https://hashnode.com/@akashrajum)

[Oct 3, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f78abb69c3b6e4101216a5b)

This is invaluable information, Thank you so much!

[Gal Malachi](https://hashnode.com/@galm)

[Oct 3, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f78abb69c3b6e4101216a5b#reply-5f78acb09c3b6e4101216a6d)

I’m glad that you find it useful, [Akash Raju M](https://hashnode.com/@akashrajum)!

[Lihai Ben-Haim](https://hashnode.com/@lihaibh)

[Oct 3, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f7837917d160d41227d7363)

Hey Gal thank you for this amazing thread ive learn alot. However i have some questions:

1.  How JWT in memory protects against XSS attacks as if XSS attack occurs an attacker can access all the resources a client has.
2.  On a "refresh-token" call i saw you are creating both refresh token and an access token, this can eventually increase the login expiration time to indefinitely as we call "refresh\_token" before the access token expires and the access token should have short expiry time.
3.  In case of a common server <-> client communication (not micro services) i dont see the reason why to encrypt the access\_token. You should not read the jwt payload on the client side?

[Gal Malachi](https://hashnode.com/@galm)

[Oct 3, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f7837917d160d41227d7363#reply-5f7841f47d160d41227d743c)

Hi [Lihai Ben-Haim](https://hashnode.com/@lihaibh),

Glad you like it! Let me answer your questions:

1.  You are right, it is still not bulletproof, however, it makes it much difficult to still the token from the memory, as the attack should be tailored to your exact app. Combining that with a short-living tokens makes the risk low, but still exists.
2.  The limit come from the refresh token expiration. When the user is active and logged in, we don't want revoke his access. If we want to limit the time for an active user, we can add a logic on the client side or the backend to force logout after a certain period. I didn’t took care of it here, but it is completely possible.
3.  encrypting the token is a good practice as it contains the identifier of the user inside it, and if stolen can give the hacker data that might be used in future attacks.

[Gal Malachi](https://hashnode.com/@galm)

[Sep 29, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f733316f955ec0a130f6374)

Part 2 was just published: [blog.galmalachi.com/react-and-jwt-authentication-…](https://blog.galmalachi.com/react-and-jwt-authentication-the-right-way-ckfnzadyi01d00ks18f7aa7ca)

[med el khalki](https://hashnode.com/@med216)

[Sep 4, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f52c39b3c1c7b7c69017384)

great work

[med el khalki](https://hashnode.com/@med216)

[Sep 4, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f52c39b3c1c7b7c69017384#reply-5f52c522fee30a7c48c0a016)

waiting for next part :)

[Tal Schoenfeld](https://hashnode.com/@TalS)

[Sep 4, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f52557941f3f2780dd65db5)

Great article!

[Gal Malachi](https://hashnode.com/@galm)

[Sep 4, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f52557941f3f2780dd65db5#reply-5f5259ca41f3f2780dd65dde)

Thanks for the feedback Tal!

[Nimrod Kramer](https://hashnode.com/@nimrodkramer)

[Sep 4, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f524fcadc2d4277eccb4158)

Well done! Very interesting read ✌🏻

[Gal Malachi](https://hashnode.com/@galm)

[Sep 4, 2020](https://hashnode.com/posts/react-nodejs-and-jwt-authentication-the-right-way/5f4ff0aec48c6b29cd2ecd0a/comment/5f524fcadc2d4277eccb4158#reply-5f52528441f3f2780dd65d85)

thanks, Nimrod, appreciate it.
