signInByEmail method

Future<AuthResponse<T>> signInByEmail(
  1. EmailAuthenticator authenticator, {
  2. SignByBiometricCallback<T>? onBiometric,
  3. Object? args,
  4. String? id,
  5. bool notifiable = true,
})

Implementation

Future<AuthResponse<T>> signInByEmail(
  EmailAuthenticator authenticator, {
  SignByBiometricCallback<T>? onBiometric,
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  emit(
    const AuthResponse.loading(Provider.email, AuthType.login),
    args: args,
    id: id,
    notifiable: notifiable,
  );

  try {
    final hasAnonymous = this.hasAnonymous;
    final response = await delegate.signInWithEmailNPassword(
      authenticator.email,
      authenticator.password,
    );
    if (!response.isSuccessful) {
      return emit(
        AuthResponse.failure(
          response.error,
          provider: Provider.email,
          type: AuthType.login,
        ),
        args: args,
        id: id,
        notifiable: notifiable,
      );
    }

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

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

    final value = await _update(
      id: user.id,
      hasAnonymous: hasAnonymous,
      onBiometric: onBiometric,
      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.email.id,
        AuthKeys.i.username: authenticator.username,
        AuthKeys.i.verified: result.emailVerified,
      },
    );

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