verifyOtp method

Future<UserCredential?> verifyOtp({
  1. required String smsCode,
  2. String verificationId = '',
  3. bool updateProfile = false,
  4. String? displayName,
  5. String? photoURL,
  6. bool linkWithAnonymousAccount = false,
})

A FirebaseAuthException maybe thrown with the following error code:

  • account-exists-with-different-credential:
  • Thrown if there already exists an account with the email address asserted by the credential. Resolve this by calling fetchSignInMethodsForEmail and then asking the user to sign in using one of the returned providers. Once the user is signed in, the original credential can be linked to the user with linkWithCredential.
  • invalid-credential:
  • Thrown if the credential is malformed or has expired.
  • operation-not-allowed:
  • Thrown if the type of account corresponding to the credential is not enabled. Enable the account type in the Firebase Console, under the Auth tab.
  • user-disabled:
  • Thrown if the user corresponding to the given credential has been disabled.
  • user-not-found:
  • Thrown if signing in with a credential from EmailAuthProvider.credential and there is no user corresponding to the given email.
  • wrong-password:
  • Thrown if signing in with a credential from EmailAuthProvider.credential and the password is invalid for the given email, or if the account corresponding to the email does not have a password set.
  • invalid-verification-code:
  • Thrown if the credential is a PhoneAuthProvider.credential and the verification code of the credential is not valid.
  • invalid-verification-id:
  • Thrown if the credential is a PhoneAuthProvider.credential and the verification ID of the credential is not valid.id.

Implementation

Future<UserCredential?> verifyOtp({
  required String smsCode,
  String verificationId = '',
  bool updateProfile = false,
  String? displayName,
  String? photoURL,
  bool linkWithAnonymousAccount = false,
}) async {
  try {
    PhoneAuthCredential credential = PhoneAuthProvider.credential(
        verificationId:
            verificationId.isEmpty ? this.verificationId! : verificationId,
        smsCode: smsCode);
    UserCredential creds;
    if (linkWithAnonymousAccount &&
        _auth.currentUser != null &&
        (_auth.currentUser?.isAnonymous ?? false)) {
      creds = await _auth.currentUser!.linkWithCredential(credential);
    } else {
      creds = await _auth.signInWithCredential(credential);
    }
    if (creds.user != null) _errorStreamController.sink.add(null);
    if (updateProfile) {
      await creds.user?.updateDisplayName(displayName);
      await creds.user?.updatePhotoURL(photoURL);
    }
    return creds;
  } on FirebaseAuthException catch (e) {
    switch (e.code) {
      case 'account-exists-with-different-credential':
        _errorStreamController.sink.add('Phonenumber already in use');
        break;
      case 'invalid-credential':
        _errorStreamController.sink.add('Credential provided are invalid');
        break;
      case 'user-disabled':
        _errorStreamController.sink.add('Account banned');
        break;
      case 'invalid-verification-code':
        _errorStreamController.sink.add('OTP provided is Invalid');
        break;
      default:
        _errorStreamController.add('Error occured. Contact admin!!');
        break;
    }
  }
}