Flutter How-To: Always allow the user to choose an account during social login with Firebase

captionless image

Hi,

My application Birikim contains Google and Apple sign-in features. I use Firebase for this feature and It is working fine.

When you login with Google or Apple account, your account is being saved and the next time you don’t need to choose your account again.

For the users, it is very helpful, and it makes the logging process much faster. But if you want to test your application with different accounts, it will be challenging.

There is a way to choose the account every time. I did a lot of searching and I found the way. I want to share with you because I don’t want you spend too much time like me :)

Google Sign-In

For Google, we need to set the “prompt” custom parameter with the “select_account” value to choose an account every time. I do that in debug mode. If you want, you can set that parameter permanently.

  late JobResultAsync<UserCredential> googleLoginJob = JobResultAsync(
    () async {
      final googleProvider = GoogleAuthProvider();

      if (kDebugMode) {
        googleProvider.setCustomParameters({"prompt": 'select_account'});
      }

      googleProvider.addScope('email');
      googleProvider.addScope('openid');
      googleProvider.addScope('profile');

      return FirebaseAuth.instance.signInWithProvider(googleProvider);
    },
    onBefore: () {
      AppDialogManager.block();
    },
    onEnd: (result) {
      validateEmailJob(result);
    },
    onError: (errorResult) {
      AppDialogManager.unblock();

      final error = errorResult.error;
      if (error is FirebaseAuthException) {
        if (error.message?.toLowerCase().contains("cancel") == false) {
          AppCore.error(error);
        }
      } else {
        AppCore.error(error);
      }
    },
  );

Apple Sign-In

For Apple, it is a little bit different. We need to revoke the authorization token. We can’t use the “prompt” parameter for Apple. There is a “revokeTokenWithAuthorizationCode” method just for Apple. We need to call it with the token that we get from sign-in.

This method will send a mail to the user to inform the user about revoking. You should be aware of it.

late JobPureAsync revokeAppleTokenJob = JobPureAsync(
  () async {
    if (kDebugMode == false) return;
    
    final token = appleLoginJob.value?.additionalUserInfo?.authorizationCode;
    
    if (Platform.isIOS && token != null && token.isNotEmpty) {
      await FirebaseAuth.instance.revokeTokenWithAuthorizationCode(token);
    }
  },
  logger: AppCore.error,
);

With this way, I can test the application with different accounts. I use that just for debug mode. You can prefer to use this feature for the release versions too.

What is the job?

Job is a package that I develop and It is not public unfortunately. I write almost all my codes in Jobs, and there are a lot of features about them. You look at this post if you wonder.

https://www.linkedin.com/feed/update/urn:li:activity:7153746048362962944

Thank you.

September 14, 2024

© 2024

Email LinkedIn Medium