Bernhard
Bernhard

Reputation: 289

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;
}

Upvotes: 0

Views: 579

Answers (2)

Bernhard
Bernhard

Reputation: 289

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

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599906

I tried reproducing the problem with this minimal code:

var cred = await signInWithGoogle();
var user = cred.user!;
print(user);

The signInWithGoogle function there is a copy/paste from the Firebase documentation on Signing in with Google on the web:

Future<UserCredential> signInWithGoogle() async {
  // Create a new provider
  GoogleAuthProvider googleProvider = GoogleAuthProvider();

  googleProvider.addScope('https://www.googleapis.com/auth/contacts.readonly');
  googleProvider.setCustomParameters({
    'login_hint': '[email protected]'
  });

  // Once signed in, return the UserCredential
  return await FirebaseAuth.instance.signInWithPopup(googleProvider);

  // Or use signInWithRedirect
  // return await FirebaseAuth.instance.signInWithRedirect(googleProvider);
}

Runnable version here: https://zapp.run/edit/auth-is-google-auto-verified-zrw06uvrx06?file=lib/main.dart

When I select my gmail account, it shows that my email is verified in the Logs panel:

User(displayName: Frank van Puffelen, email: [email protected], emailVerified: true, isAnonymous: false, metadata: ...

When you run the same example, does it show emailVerified as false?

Upvotes: 1

Related Questions