login method

Future<AuthResponse> login({
  1. required String email,
  2. required String password,
})

Authenticates a user using an email and password.

If the user has multi-factor authentication enabled, the returned AuthResponse will only have its AuthResponse.mfa field set, which can then be used to complete the login via completeMfaLogin alongside the user's one-time-password.

Throws an ApiException if login fails.

https://docs.nhost.io/auth/api-reference#login-user

Implementation

Future<AuthResponse> login({
  required String email,
  required String password,
}) async {
  log.finer('Attempting login');
  AuthResponse? loginRes;
  try {
    loginRes = await _apiClient.post(
      '/login',
      data: {
        'email': email,
        'password': password,
        'cookie': false,
      },
      responseDeserializer: AuthResponse.fromJson,
    );
    log.finer('Login successful');
  } catch (e, st) {
    log.finer('Login failed', e, st);
    await _clearSession();
    rethrow;
  }

  if (loginRes!.mfa != null) {
    return loginRes;
  }

  await setSession(loginRes.session!);
  return loginRes;
}