Storing User-Id in Client Side App for API Request Storing User-Id in Client Side App for API Request express express

Storing User-Id in Client Side App for API Request


You're right about option #1. Never decode the token client-side. That would require the client-side code to know the "secret", which would expose it to anyone looking through your Javascript.

Option #2 is good, assuming that you still send the token with every request for security purposes. For storage, yes you have to store it in a cookie or in localStorage, or as you say it will be lost on refresh.

To get the ID in the client-side code, have your client-side code read it from the cookie / localstorage. There are libraries for that; react-cookie reads cookies, for example. Either you can do that every time you need to access it, or you can read it once during the initial page load, and then dispatch it into the Redux store.


In your /users/:id endpoint you can check to see if an :id was provided and then if not, extract the id from your JWT token otherwise use the id that was passed into the API call.

If that isn't acceptable, then you could use option #2 but use sessionStorage instead of localStorage