signInByBiometric method

  1. @override
Future<AuthResponse<T>> signInByBiometric({
  1. BiometricConfig? config,
})
override

Implementation

@override
Future<AuthResponse<T>> signInByBiometric({
  BiometricConfig? config,
}) async {
  try {
    emit(const AuthResponse.loading(
      AuthProviders.biometric,
      AuthType.biometric,
    ));
    final user = await _auth;
    if (user != null && user.isBiometric) {
      final response = await authHandler.signInWithBiometric(config: config);
      if (response.isSuccessful) {
        final token = user.accessToken;
        final provider = AuthProviders.from(user.provider);
        var current = Response<UserCredential>();
        if ((user.email ?? user.username ?? "").isNotEmpty &&
            (user.password ?? '').isNotEmpty) {
          if (provider.isEmail) {
            current = await authHandler.signInWithEmailNPassword(
              email: user.email ?? "",
              password: user.password ?? "",
            );
          } else if (provider.isUsername) {
            current = await authHandler.signInWithUsernameNPassword(
              username: user.username ?? "",
              password: user.password ?? "",
            );
          }
        } else if ((token ?? user.idToken ?? "").isNotEmpty) {
          if (provider.isApple) {
            current = await authHandler.signInWithCredential(
              credential: OAuthProvider("apple.com").credential(
                idToken: user.idToken,
                accessToken: token,
              ),
            );
          } else if (provider.isFacebook) {
            current = await authHandler.signInWithCredential(
              credential: FacebookAuthProvider.credential(token ?? ""),
            );
          } else if (provider.isGoogle) {
            current = await authHandler.signInWithCredential(
              credential: GoogleAuthProvider.credential(
                idToken: user.idToken,
                accessToken: token,
              ),
            );
          }
        }
        if (current.isSuccessful) {
          final data = <String, dynamic>{
            AuthKeys.i.loggedIn: true,
            AuthKeys.i.loggedInTime: Entity.generateTimeMills,
          };
          return _update(
            id: user.id,
            creates: data,
            updates: data,
          ).then((value) {
            return emit(AuthResponse.authenticated(
              value,
              msg: msg.signInWithBiometric.done,
              provider: AuthProviders.biometric,
              type: AuthType.biometric,
            ));
          });
        } else {
          return emit(AuthResponse.failure(
            current.exception,
            provider: AuthProviders.biometric,
            type: AuthType.biometric,
          ));
        }
      } else {
        return emit(AuthResponse.failure(
          response.exception,
          provider: AuthProviders.biometric,
          type: AuthType.biometric,
        ));
      }
    } else {
      return emit(AuthResponse.failure(
        msg.biometric,
        provider: AuthProviders.biometric,
        type: AuthType.biometric,
      ));
    }
  } catch (_) {
    return emit(AuthResponse.failure(
      msg.signInWithBiometric.failure ?? _,
      provider: AuthProviders.biometric,
      type: AuthType.biometric,
    ));
  }
}