mercredi 1 juillet 2020

How to do autologin with three diffrent userTypes in flutter and firebase?

I have this app that do login with firebase auth and firestore to get the userType, This code is written obviously in the login page, What I want to add is autologin ASA the app runs which firebase offers with the correct userType So the first proplem how to transfer the email value to the main.dart page as I search in the firestore with the email to get the userType, Second proplem is that When I tried to do auto login in the login page with three different userTypes It does login but not auto login CODE :

@override
  void initState() {
    super.initState();
    FirebaseAuth.instance.currentUser().then(
      (result) {
        if (result != null) {
          if (userType == 'userType1') {
            Navigator.pushReplacementNamed(context, '/userType1page');
          }
          if (userType == 'userType2') {
            Navigator.pushReplacementNamed(context, '/userType2page');
          }
          if (userType == 'userType3') {
            Navigator.pushReplacementNamed(context, '/userType3page');
          }
        }

So Here It gets the user But no auto login, what I observed that When U remove the other IFs inside the big if and do 1 Navigation It works So don't know what to do, Please Help me I asked three questions before and didn't get an answer.

PS : NEW TO FLUTTER :)

#FLUTTER_FOREVER

EDIT:

void getUserData() async {
    try {
      firestoreInstance
          .collection('Users')
          .document(usernameController.text)
          .get()
          .then((value) {
        setState(() {
          email = (value.data)['email'];
          password = (value.data)['password'];
          gender = (value.data)['gender'];
          username = (value.data)['username'];
          userType = (value.data)['userType'];
        });
      });
    } catch (e) {
      print(e.toString);
    }
  }

Second EDIT :

void login() async {
    final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
    firebaseAuth
        .signInWithEmailAndPassword(
            email: emailController.text, password: passwordController.text)
        .then((result) {
      {
        if (userType == 'Student') {
          Navigator.pushReplacementNamed(context, '/StudentsPage');
        } else if (userType == 'Teacher') {
          Navigator.pushReplacementNamed(context, '/TeacherPage');
        } else if (userType == 'Admin') {
          Navigator.pushReplacementNamed(context, '/AdminPage');
        } else {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Error'),
                content: Text(
                    'Please make sure that you have an internet connection '),
                actions: [
                  FlatButton(
                    child: Text("Ok"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  )
                ],
              );
            },
          );
        }
      }

Aucun commentaire:

Enregistrer un commentaire