Home > database >  react native asyncstorage, how to create unique data for each user
react native asyncstorage, how to create unique data for each user

Time:03-18

been working on this app with react native and have been implementing persistence for some functionality with Async Storage.... but a big detail kinda flew over my head.

For the user to use the app they have to log in, and it works perfectly for one user, all data is kept and all..... but if on the same device, a different user logs in then I get the data stored in the async storage from the previews user... which is kinda of obvious, but didn't even think of that.

I can't seem to think of a way I could differ that info for different users? the only data I could use to differ each user would be the email they use to log in... would I have to create a different entry on the storage with the email as a key and then do a logic to check if it is theirs or some other users info?

any ideas on the best practice for this situation?

Thank you in advance

CodePudding user response:

You could make your keys for different users distinct by starting each key with a user's email. Example:

Joe:

AsyncStorage.setItem('[email protected]/userId', jsonValue)

James:

AsyncStorage.setItem('[email protected]/userId', jsonValue)

Another option is that every time a user logs out of your app you could clear the AsyncStorage using await AsyncStorage.clear().

  • Related