signInByEmail method

  1. @override
Future<AuthResponse<T>> signInByEmail(
  1. EmailAuthenticator authenticator, {
  2. SignByBiometricCallback? onBiometric,
})
override

Implementation

@override
Future<AuthResponse<T>> signInByEmail(
  EmailAuthenticator authenticator, {
  SignByBiometricCallback? onBiometric,
}) async {
  final email = authenticator.email;
  final password = authenticator.password;
  if (!AuthValidator.isValidEmail(email)) {
    return emit(AuthResponse.failure(
      msg.email,
      provider: AuthProviders.email,
      type: AuthType.login,
    ));
  } else if (!AuthValidator.isValidPassword(password)) {
    return emit(AuthResponse.failure(
      msg.password,
      provider: AuthProviders.email,
      type: AuthType.login,
    ));
  } else {
    try {
      emit(const AuthResponse.loading(AuthProviders.email, AuthType.login));
      final response = await authHandler.signInWithEmailNPassword(
        email: email,
        password: password,
      );
      if (response.isSuccessful) {
        final result = response.data?.user;
        if (result != null) {
          final user = authenticator.copy(
            id: result.uid,
            email: result.email,
            name: result.displayName,
            phone: result.phoneNumber,
            photo: result.photoURL,
            provider: AuthProviders.email.name,
            loggedIn: true,
            loggedInTime: Entity.generateTimeMills,
          );
          if (onBiometric != null) {
            final biometric = await onBiometric(user.mBiometric);
            return _update(
              id: user.id,
              creates: user.copy(biometric: biometric?.name).source,
              updates: {
                ...user.extra ?? {},
                AuthKeys.i.loggedIn: true,
                AuthKeys.i.loggedInTime: Entity.generateTimeMills,
              },
            ).then((value) {
              return emit(AuthResponse.authenticated(
                value,
                msg: msg.signInWithEmail.done,
                provider: AuthProviders.email,
                type: AuthType.login,
              ));
            });
          } else {
            return _update(
              id: user.id,
              creates: user.source,
              updates: {
                ...user.extra ?? {},
                AuthKeys.i.loggedIn: true,
                AuthKeys.i.loggedInTime: Entity.generateTimeMills,
              },
            ).then((value) {
              return emit(AuthResponse.authenticated(
                value,
                msg: msg.signInWithEmail.done,
                provider: AuthProviders.email,
                type: AuthType.login,
              ));
            });
          }
        } else {
          return emit(AuthResponse.failure(
            msg.authorization,
            provider: AuthProviders.email,
            type: AuthType.login,
          ));
        }
      } else {
        return emit(AuthResponse.failure(
          response.exception,
          provider: AuthProviders.email,
          type: AuthType.login,
        ));
      }
    } catch (_) {
      return emit(AuthResponse.failure(
        msg.signInWithEmail.failure ?? _,
        provider: AuthProviders.email,
        type: AuthType.login,
      ));
    }
  }
}