flutterfirebase-authenticationgoogle-signinmulti-factor-authenticationemail-verification

verify email-adress from google sign-up - flutter mobile app


I try to add an multi-factor auth to an existing account, which is signed-in with google.

I get the error "verify email first":

[SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17086 Need to verify email first before enrolling second factors.

So it seems like I have to verify the google email-adress. My question is whether i have to authenticate the user again via email and password sign-in. With:

final credential = EmailAuthProvider.credential(email: emailAddress, password: password);

And then link the google and email auth providers, as described here: https://firebase.google.com/docs/auth/flutter/account-linking?authuser=0

Or do I have to use "Email Link Authentication", as described here: https://firebase.google.com/docs/auth/flutter/email-link-auth?authuser=0

Or is there a completely different, easier way to verify the email address of the google account?

used code:

final GoogleSignIn googleSignIn = GoogleSignIn();

Future<UserCredential> signInWithGoogle(BuildContext context) async {
  // Trigger the authentication flow
  final GoogleSignInAccount? googleUser = await (googleSignIn.signIn());

  // Obtain the auth details from the request
  final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;

  // Create a new credential
  final credential = GoogleAuthProvider.credential(
    accessToken: googleAuth?.accessToken,
    idToken: googleAuth?.idToken,
  );

  // Once signed in, return the UserCredential
  final UserCredential authResult = await FirebaseAuth.instance.signInWithCredential(credential);

  String? name = authResult.user?.displayName;
  final String? email = authResult.user?.providerData[0].email ;
  final String? imageUrl = authResult.user?.photoURL;

  //Only taking the first part of the name, i.e., First Name
  if (name!.contains(" ")) {
    name = name.substring(0, name.indexOf(" "));
  }

  print(name);
  print(email);
  print(imageUrl);
  print(authResult.user?.emailVerified);

  return authResult;
}

Solution

  • you have to additionally verify the email address of a google account via "Authenticate with Firebase Using Email Links" https://firebase.google.com/docs/auth/flutter/email-link-auth?authuser=0

    Works fine for me