DEV Community

Discussion on: Remaining Stateless - JWT + Cookies in Node JS(REST)

Collapse
 
belhassen07 profile image
Belhassen Chelbi

The best way to store JWT is the memory, while having an HTTP ONLY cookie containing the refresh token.

Collapse
 
reynarow profile image
reza

i heard that many time. but how's the implementation?

Collapse
 
mylastore profile image
Oscar Quinteros

Write an example just like Ogbonna Basil ;)

Collapse
 
mr_cea profile image
Ogbonna Basil

Thanks Belhassen. I will take a look at this method too.

Collapse
 
zeeessdoubleu profile image
Zak Williams

Belhassen, when you say store the JWT in memory, are you talking about localstorage, or are you talking about something like Redux or React Context state?

Collapse
 
belhassen07 profile image
Belhassen Chelbi

In the application state (eg: react state).

Thread Thread
 
ghison profile image
Beman Ghison (Behnam Ghiaseddin)

And when user close the browser? Of course, some applications (like banking) prefer to terminate user login, but on most applications, we need to keep user login after closing the browser and open it next time.

Thread Thread
 
belhassen07 profile image
Belhassen Chelbi

the http only cookie for the refresh token stays so you can always get the acces token accessing the refresh endpoint that will give you a new access and a new refresh token so no need to login again.