verifyPhoneByOtp method

Future<AuthResponse<T>> verifyPhoneByOtp(
  1. OtpAuthenticator authenticator
)
inherited

Implementation

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

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

    final result = response.data;
    final uid = result?.uid ?? '';
    if (result == null || uid.isEmpty) {
      return AuthResponse.failure(
        msg.authorization,
        type: AuthType.phone,
      );
    }

    return AuthResponse<T>.authenticated(
      _backup.build({
        keys.id: uid,
        keys.loggedIn: true,
        keys.loggedInTime: EntityHelper.generateTimeMills,
        keys.provider: 'PHONE_NUMBER',
        keys.phone: result.phoneNumber,
        keys.verified: true,
      }),
      msg: msg.signInWithPhone.done,
      type: AuthType.phone,
    );
  } catch (error) {
    return AuthResponse.failure(
      msg.signInWithPhone.failure ?? error.toString(),
      type: AuthType.phone,
    );
  }
}