signInByOtp method

Future<AuthResponse<T>> signInByOtp(
  1. OtpAuthenticator authenticator, {
  2. bool storeToken = false,
  3. Object? args,
  4. String? id,
  5. bool notifiable = true,
})

Implementation

Future<AuthResponse<T>> signInByOtp(
  OtpAuthenticator authenticator, {
  bool storeToken = false,
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  emit(
    const AuthResponse.loading(Provider.phone, AuthType.phone),
    args: args,
    id: id,
    notifiable: notifiable,
  );

  try {
    final hasAnonymous = this.hasAnonymous;
    final credential = delegate.credential(
      Provider.phone,
      Credential(
        smsCode: authenticator.smsCode,
        verificationId: authenticator.verificationId,
      ),
    );

    final response = await delegate.signInWithCredential(credential);

    if (!response.isSuccessful) {
      return emit(
        AuthResponse.failure(
          response.error,
          provider: Provider.phone,
          type: AuthType.phone,
        ),
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

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

    final user = authenticator.update(
      id: Modifier(result.uid),
      accessToken:
          storeToken ? Modifier(result.accessToken) : Modifier.nullable(),
      idToken: storeToken ? Modifier(result.idToken) : Modifier.nullable(),
      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),
    );

    final value = await _update(
      id: user.id,
      hasAnonymous: hasAnonymous,
      initials: user.filtered,
      updates: {
        if (authenticator.extra != null) ...authenticator.extra!,
        AuthKeys.i.loggedIn: true,
        AuthKeys.i.loggedInTime: EntityHelper.generateTimeMills,
        AuthKeys.i.anonymous: result.isAnonymous,
        AuthKeys.i.email: result.email,
        AuthKeys.i.name: result.displayName,
        AuthKeys.i.password: authenticator.password,
        AuthKeys.i.phone: result.phoneNumber,
        AuthKeys.i.photo: result.photoURL,
        AuthKeys.i.provider: Provider.phone.id,
        AuthKeys.i.username: authenticator.username,
        AuthKeys.i.verified: result.emailVerified,
      },
    );

    return emit(
      AuthResponse.authenticated(
        value,
        msg: msg.signInWithPhone.done,
        provider: Provider.phone,
        type: AuthType.phone,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  } catch (error) {
    return emit(
      AuthResponse.failure(
        msg.signInWithPhone.failure ?? error,
        provider: Provider.phone,
        type: AuthType.phone,
      ),
      args: args,
      id: id,
      notifiable: notifiable,
    );
  }
}