lohanidamodar / flutter_firebase_starter

Flutter firebase starter project
MIT License
359 stars 72 forks source link

User management \ roles #2

Closed ferrumzp closed 4 years ago

ferrumzp commented 4 years ago

Hello. Thank you very, very much for your work. This is a very good start for beginners and those who are just starting to learn Flutter like me. I want to ask you how can I make authorization for user types: user, manager, admin? I only found heavily outdated data that doesn't support the current version of Dart and Flutter. And how to make sure that the authorized user remains in the system on subsequent launches of the application? Thank you. Best regards, Dmitry

DUKITROX commented 4 years ago

Hey, I also started learning flutter and firebase quite recently, but I think you can not manage roles just with firebase authenticaiton. Instead, what you could do, is make a some sort of launch screen, where you would give the user three options: signUp as user, manager or admin, and from there redirect them to a screen that implements the authorization methods for the user type selected. Also, in here, you could try and use firestore or realtime database, to store the user type and other authentication requirements.

And for the second question, you could use a library called "Shared Preferences" which allows you to store data locally with key value pairs, which basiclly means you can save weather the user has logged in or not, and in case the user is logged in, you can instead of displaying the initial "SignUp screen" you could directly redirect them to your app. Also, if you want to store more complex data locally from the user, you might want to use "SQLite" which allows you to manage a local database. Hope it was somehow helpful.

Daniel from Spain

lohanidamodar commented 4 years ago

@DUKITROX thank you for you detailed explaination. Yes most of the part its correct. But @ferrumzp consider following things

  1. Firebase auth doesn't have role management, I suggest to use firestore to keep the type of user (you can allow user to choose type themselves or admin assigns the type which is stored in the firestore document for the user based on the requirement of your application)
  2. If you check the code, when user signs up for the first time, I have created user record in firestore and there I have saved various details, like users name, email, device details, registration date etc. You can save the user type similarly.
  3. When the user logs in again, based on the type stored in firestore, you can then redirect the user to appropriate functionality
  4. With firebase authentication, you can test that once you log in, until you logout, you remain logged in, that happens automatically, you don't have to do any more setup. so no need to use local database or shared prefs yoursefl.

Hope this answered your queries. Thank you