ROLE BASED AUTHORIZATION AND ROLE BASED ACCESS CONTROLL !! flutter/firebase

435 views
Skip to first unread message

Error Place

unread,
Mar 10, 2020, 8:41:44 AM3/10/20
to Flutter Development (flutter-dev)
Hey guys!

what im trying to do is to setup a role based authorization (reqular users and subscribed users) and based on roles users gets redirected to different screens. i am STUCK. ive tried different solutions and seen every tutorial there is out there about the concept. i understand how the concept works but having a realy hard time setting it up in my code. im not sure where to declare the subscribed users and how create the function and how to Navigate them! 
greatful for any help! this is how my code looks!


//this is my auth services

 static void signUpUser(
  BuildContext context, String name, String email, String password) async {
try {
  AuthResult authResult = await _auth.createUserWithEmailAndPassword(
      email: email,
      password: password
  );
  FirebaseUser signedInUser = authResult.user;
  if (signedInUser != null) {
    _firestore.collection('/users').document(signedInUser.uid).setData({
      'name': name,
      'email': email,
      'profileImageUrl': '',
    });

//sign up page
 final _formKey = GlobalKey<FormState>();
  String _name, _email, _password;

  _submit() {
    if(_formKey.currentState.validate()){
      _formKey.currentState.save();

      AuthService.signUpUser(context, _name, _email, _password);
    }
  }

//my main.dart
 Widget _getScreenId() {
    return StreamBuilder<FirebaseUser>(
      stream: FirebaseAuth.instance.onAuthStateChanged,
      builder: (BuildContext context, snapshot) {
        if (!snapshot.hasData) {
          Provider.of<UserData>(context).currentUserId = snapshot.data.uid;
          return LoginScreen();
        } else {
          return HomeScreen();
        }
      },
    );
  }


tomohiro suzuki

unread,
Mar 11, 2020, 4:39:18 PM3/11/20
to Error Place, Flutter Development (flutter-dev)
Which part is not working as expected?

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/8fcd8fef-e7b6-49a4-b6fc-4ff4d6cae88a%40googlegroups.com.

tomohiro suzuki

unread,
Mar 12, 2020, 9:00:51 AM3/12/20
to Error Place, Flutter Development
save isAdmin in the document. In the same way you do in Biografy.

Sent from my iPad

On Mar 12, 2020, at 7:34 AM, Error Place <reza7....@gmail.com> wrote:


Ive nog created the Update function, so my users can update their information 
     Class DatabaseService {
      static void updateUserData(User user){
      UsersRef.document(user.id).updateData({
      Name .......
      Profile image ........
       Biografy.......
});
}
But I still don’t know how to set the isAdmin = true;
I want the user to be able to choose admin! Or regular user in the signup page and then based on the chosen role get directed to different screens!
Thanks in advance 🙏


tors 12 mars 2020 kl. 00:02 skrev Error Place <reza7....@gmail.com>:
Thank you. I‘be already gone through the docs , but I guess I missed that. I will give it a try🙏🙏

ons 11 mars 2020 kl. 23:55 skrev tomohiro suzuki <tomotom...@gmail.com>:
Good. Then you need to know how to save a document per user. Very similar to 
_firestore.collection('/users').document(signedInUser.uid).setData({
      'name': name,
      'email': email,
      'profileImageUrl': '',
    })
But with “updateData” method this time. For difference between setData and updateData:

Sent from my iPad

On Mar 11, 2020, at 6:36 PM, Error Place <reza7....@gmail.com> wrote:


Thank you for your response.
My app runs on both iOS and Android, and it also signs up user. But it doesn’t assign roles to user  though I don’t know how to write the code that makes it happen! I can set user roles manually in the firebase console but don’t know how to figure out the navigation either.
Best regards

ons 11 mars 2020 kl. 22:08 skrev Suzuki Tomohiro <tomotom...@gmail.com>:
Does your app run in yours device (iOS/Android/Web)?
Does your app let your user sign-in?
Does your app assign a role to a user?
Does your users see different pages by the role?

On Wed, Mar 11, 2020 at 4:46 PM Error Place <reza7....@gmail.com> wrote:
What I’m would like to achieve is that users with admin role get special UI and access to every page
While regular user only can  see some pages.
Best regards

Felix

unread,
Mar 13, 2020, 1:56:53 AM3/13/20
to Flutter Development (flutter-dev)
You will need to learn about custom claims.

Try going through this tutorials:

cheers.
Reply all
Reply to author
Forward
0 new messages