2

i'm trying to learn redux and i'm not sure what is best way to structure my app state when the app is used by multiple users.

App flow:

on every login {userid,username,token} is stored in localStorage.appUsers

now if my app initialState looks like following

{
 appusers:[],//Array of app users, i only keep the token and id and username and i fetch the full user info from server when user is selected as current logged in.
 currentUser:{id,fullname,picture,...etc} //current user
 blog:[],//array of blog posts of current logged in user
 chat:[],//array of chat messages of current logged in user
}

i use localstorage also to save the user state so that next time he open app, it present initialState from localstorage until it refresh the cache from server.

problem is that currently the blog and chat keeps array of posts for the current logged in user, when user switch users he is presented with someone else data.

so what is the correct approach to maintain separate store for every user ? i tried to modify my initial state so that the blog and chat arrays become a property of appUsers, then i faced problem with react combineReducers which expects a key for every reducer in the initialState.

Zalaboza
  • 8,899
  • 16
  • 77
  • 142
  • 1
    I wouldn't advise keeping data around (in `state`) for users that are no longer logged in. – lux Aug 08 '16 at 17:39
  • @lux my users usually have multiple accounts on same device :/, so i need to keep data to improve ux next time he switch back – Zalaboza Aug 08 '16 at 17:50
  • I would only ever have one account logged in at one time. Perhaps you could keep track of the other available accounts that can be chosen from, but with only account being used by the store at any given time. I wouldn't keep any data associated with the account in localStorage, not *after* the user has logged out. Local storage is visible to any user and that could be a security risk. Also, saving the state to the server instead of localStorage allows that state to be accessed from other devices as well. Like, start an action on the computer, and resume that action from my phone. – Dustin Stiles Aug 08 '16 at 18:02
  • Have you seen this example of isolating sub apps? http://redux.js.org/docs/recipes/IsolatingSubapps.html – horyd Aug 09 '16 at 14:42
  • I think you are asking "how to reset state when user switch account". Check here to see the solution: http://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store – Ming Sep 05 '16 at 04:22

0 Answers0