Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to correctly store a user's jwt token on React

What is the best, most secure and professional way to store a user’s jwt token after logging into React?

I see many people saying that using localStorage is a good way.

For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

localStorage.setItem("token", "ey.......")

Others say to use a library like Redux or others.

Could someone advise me?

Thanks

>Solution :

Redux hasn’t built-in persistent storage. It means on refresh of the page your key might be lost, and you need to re-login(authorize) once again. There is no "correct" way, there is "desired behaviour".

As already was suggested to you in comments you can use also cookies as a storage of the key, and I think it is one of the most preferable ways for now, as it is kinda safe solution.

Redux has middleware to persist its state. You can choose there what kind of storage you want to use as a long-term storage.

But, I wouldn’t recommend you add redux to the project just to have it.

And there is a good answer on difference between most popular browser storages.

Read carefully and choose smart, there are some major differences like scope and secure between them.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading