2

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();
        }
      },
    );
  }

//user models import 'package:cloud_firestore/cloud_firestore.dart';

class User {
  final String id;
  final String name;
  final String profileImageUrl;
  final String email;
  final String bio;

  User({
    this.id,
    this.name,
    this.profileImageUrl,
    this.email,
    this.bio
  });
Error Place
  • 45
  • 1
  • 1
  • 8
  • "I understand how the concept works but having a really hard time setting it up in my code." Since your code doesn't show anything about user roles, it's hard to help. What specific code are you asking about? Failing that, I recommend checking out https://www.youtube.com/watch?v=oFlHzF5U-HA – Frank van Puffelen Mar 10 '20 at 13:16
  • hej frank. thank you for your response! i updated the question with my USER MODELS . to be honest im not exactly sure about how to define the users role to begin with . – Error Place Mar 10 '20 at 13:29
  • In that case the video will be a great place to start. The two most common places to store role information are: 1) as a custom claim in the Firebase Authentication token for that user, 2) in the database in a document associated with that user. No matter which one you pick, you should be setting this role from within a trusted environment (your development machine, a server you control, or Cloud Functions) as otherwise anyone can change their own role. – Frank van Puffelen Mar 10 '20 at 13:50
  • Did you try: https://github.com/casbin/casbin-dart ? – hsluoyz Jun 24 '20 at 04:47

1 Answers1

3

The two most common places to store role information are:

  1. as a custom claim in the Firebase Authentication token for that user,
  2. in the database in a document associated with that user.

No matter which one you pick, you should be setting this role from within a trusted environment (your development machine, a server you control, or Cloud Functions) as otherwise anyone can change their own role.

Once set in either of these locations, you can access the role information in your client-side code, and navigate to the correct screen for that user.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807