Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Facebook Login Allowing to switch account

The app allows the user to sign in using Facebook, but I am having trouble with when the using wants to use a different Facebook account to sign in, this page will only show the continue button. Is there a way for the user to choose to use a different account for signing in? Also, I have not really seem any error in the case for FacebookLoginStatus.error, what are some error handling that should be done to it? I am new to flutter and coding, it will be helpful to point out if anything should be done differently or can be improve on.

onPressed: () async {
        final facebookLogin = FacebookLogin();
        final result = await facebookLogin.logIn(['email']);

        switch (result.status) {
          case FacebookLoginStatus.loggedIn:
           try{
             final token = result.accessToken.token;
             AuthCredential credential =
             FacebookAuthProvider.getCredential(accessToken: token);

             FirebaseUser user = (await _auth.signInWithCredential(credential)).user;

             if(user != null){
               Navigator.pushReplacementNamed(context, Home.id);
             }

           } catch(e){
             print(e);
           }
            break;
          case FacebookLoginStatus.cancelledByUser:
            print('cancelled');
            break;
          case FacebookLoginStatus.error:
            print('error');
            break;
        }
      },
like image 466
Xian Wu Avatar asked Nov 06 '22 10:11

Xian Wu


1 Answers

having trouble with when the using wants to use a different Facebook account to sign in, this page will only show the continue button

I use FacebookLogin.logOut() to disconnect the user, just so the user can use a different account to login.

onPressed: () async {

      final facebookLogin = FacebookLogin();

      await facebookLogin.logOut(); // disconnect user

      final FacebookLoginResult result =
          await facebookLogin.logIn(['email', 'public_profile']);

        switch (result.status) {
          case FacebookLoginStatus.loggedIn:
           try{
             final token = result.accessToken.token;
             AuthCredential credential =
             FacebookAuthProvider.getCredential(accessToken: token);

             FirebaseUser user = (await _auth.signInWithCredential(credential)).user;

             if(user != null){
               Navigator.pushReplacementNamed(context, Home.id);
             }

           } catch(e){
             print(e);
           }
            break;
          case FacebookLoginStatus.cancelledByUser:
            alertMessage(context, 'cancelled'); // show dialog
            break;
          case FacebookLoginStatus.error:
            alertMessage(context, 'error'); // show dialog
            break;
        }
      },

what are some error handling that should be done to it?

You may prompt user of the login status messages:

void alertMessage(context, String message) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text("Error"),
        content: Text(message),
        actions: [
          FlatButton(
            child: Text(S.of(context).ok),
            onPressed: () {
              Navigator.of(context).pop();
            },
          )
        ],
      )
    );
  }
like image 153
termianfish Avatar answered Nov 15 '22 08:11

termianfish