login method

Future<AuthStateModel> login(
  1. String email,
  2. String password
)

Implementation

Future<AuthStateModel> login(String email, String password) async {
  var currentState = AuthStateModel.empty();

  try {
    final userModel = await _auth.signInWithEmailAndPassword(email, password);

    currentState = currentState.copyWith(
      state: AuthState.authenticated,
      user: userModel,
      errorMessage: null,
    );

    state = currentState;
  } on UserNotFoundException catch (e) {
    error('Auth error: User not found', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } on InvalidCredentialsException catch (e) {
    error('Auth error: Invalid credentials', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } on EmailAlreadyInUseException catch (e) {
    error('Auth error: Email already in use', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } on RecentLoginRequiredException catch (e) {
    error('Auth error: Recent login required', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } on AuthException catch (e) {
    error('Auth error: ${e.message}', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: e.code,
    );
    state = currentState;
  } catch (e) {
    error('Unexpected auth error: ${e.toString()}', error: e);
    currentState = currentState.copyWith(
      state: AuthState.error,
      errorMessage: AuthException.unknownCode,
    );
    state = currentState;
  }

  return state;
}