4

I'm wandering how to store user information after login?

Is it a good idea to store all the information in local storage or in redux store?

After successful login API return me a large object with user role and permission along with some other information.

I'm new in react-redux please help!

Thanks!

Tech Aimley
  • 67
  • 2
  • 7

2 Answers2

9

Store user information in redux store. If you want to persist it (I do not recommend it, see details in this answer bellow) after reload of the application, you can use redux-persist.

But it really depends on what kind of authentication are you using. Generally you should use (and persist) only some authentication token (not the all user information). You can store this token in cookies and use it (for the backend request) for getting real user information (which you then store in redux store) on each application load.

lavor
  • 1,787
  • 1
  • 14
  • 21
  • Thanks for the answer, react-persist seems to be a solution as I'm already afraid of losing store data on refresh. – Tech Aimley Nov 21 '19 at 09:16
  • Yes, `redux-persist` could help you with that. But as I mentioned, you should't trust your client storage (whether it is local storage or redux store) about the "authenicated state", especially after closing your aplication (e.g. on refresh, you are closing the old one and starting new one). Your server is the one, who should check (on each request) if you are really authenticated. And for that the authentication token (usually stored in cookies) is the right thing. – lavor Nov 21 '19 at 09:34
  • Thanks! I got your point. I just want a piece of user information in my different components, but i have no idea which is a good place to fetch the details from redux-store or localstorage. – Tech Aimley Nov 21 '19 at 09:42
  • Ok, if you have Redux store already in your application, I don't see any reason why you shouldn't store user information there. It is the same kind of data as any requested data from your backend. That I shortly wrote also in my first sentence of this answer. And if you don't have Redux store in your application yet, it is better to store user information in local storage than introducing Redux store only for that. – lavor Nov 21 '19 at 09:45
0

We can store all general data like list of cities and countries in the local storage. If you want to logout the user, when user close the tab, then you can use Session storage. So you can store the user information in session storage or in cookies.
check the following link to know the difference about local storage and session storage. Link

Ananth
  • 1,520
  • 3
  • 15
  • 28
  • Are you doing this also in applications with Redux store? If so, can I ask what are benefits of not storing it in Redux store? – lavor Nov 21 '19 at 08:52
  • Thanks for the answer, As session storage only persist data for a current tab, but i could need the user info in through the application, and is it good practice to store user information in local storage? there could be some sensitive info too. – Tech Aimley Nov 21 '19 at 09:21