verifyPhoneByOtp method

Future<AuthResponse<Auth<AuthKeys>>> verifyPhoneByOtp(
  1. OtpAuthenticator authenticator
)

Implementation

Future<AuthResponse> verifyPhoneByOtp(OtpAuthenticator authenticator) async {
  try {
    final credential = delegate.credential(
      Provider.phone,
      Credential(
        smsCode: authenticator.smsCode,
        verificationId: authenticator.verificationId,
      ),
    );

    final response = await delegate.signInWithCredential(credential);
    if (!response.isSuccessful) {
      return AuthResponse.failure(
        response.error,
        provider: Provider.phone,
        type: AuthType.phone,
      );
    }

    final result = response.data;
    if (result == null) {
      return AuthResponse.failure(
        msg.authorization,
        provider: Provider.phone,
        type: AuthType.phone,
      );
    }

    final user = authenticator.update(
      id: Modifier(result.uid),
      accessToken: Modifier(result.accessToken),
      idToken: Modifier(result.idToken),
      anonymous: Modifier(result.isAnonymous),
      email: Modifier(result.email),
      name: Modifier(result.displayName),
      phone: Modifier(result.phoneNumber),
      photo: Modifier(result.photoURL),
      provider: Modifier(Provider.phone),
      loggedIn: Modifier(true),
      loggedInTime: Modifier(EntityHelper.generateTimeMills),
      verified: Modifier(true),
    );

    return AuthResponse.authenticated(
      user,
      msg: msg.signInWithPhone.done,
      provider: Provider.phone,
      type: AuthType.phone,
    );
  } catch (error) {
    return AuthResponse.failure(
      msg.signInWithPhone.failure ?? error,
      provider: Provider.phone,
      type: AuthType.phone,
    );
  }
}