Best practice for storing user information

Viewed 171

I am making an application using Flutter that handles its login with Firebase Authentication. The authentication works fine, but I am curious as to the best practice in storing the User object.

Currently, signing up/logging in converts the database user's fields into a User object. Because of the necessity of this information app-wide, I would like to make this information accessible "globally". Of course, before the user ever modifies the database or another vital function I will ensure they are truly authenticated.

I am currently handling this by having a global library with a User singleton, but I have heard that globals, and therefor singletons, are generally a bad practice. Eventually I would like it so that the user stays authenticated even after closing the application, so I will likely end up using SharedPreferences.

Should I just use SharedPreferences for this? Should I keep using my global library and also integrate SharedPreferences? Should I somehow use Provider?

Basically two questions:

  1. What is generally the best practice in Flutter/Dart for storing the user object in the current session?
  2. What is generally the best practice in Flutter/Dart for storing the user object for future sessions?
1 Answers

In my opinion a very common practice is using shared_preferences plugin on pub.dev. But just keep in mind a security issue about this. This will be save in a unencrypted file, so if your mobile is Rooted by some 3rd party app someone could access it. So a good alternative also could be to use encrypt plugin on pub.dev and encrypt this data before save it.

Related