authenticate method

Future<UuidValue> authenticate(
  1. Session session, {
  2. required String email,
  3. required String password,
  4. required Transaction? transaction,
})

Returns the AuthUser's ID upon successful email/password verification.

Can throw the following EmailLoginServerException subclasses:

In case of invalid credentials, the failed attempt will be logged to the database outside of the transaction and can not be rolled back.

Implementation

Future<UuidValue> authenticate(
  final Session session, {
  required String email,
  required final String password,
  required final Transaction? transaction,
}) async {
  email = email.normalizedEmail;

  final attemptCount = await _rateLimitUtil.countAttempts(
    session,
    nonce: email,
    transaction: transaction,
  );
  if (attemptCount >= _maxAttempts) {
    throw EmailAuthenticationTooManyAttemptsException();
  }

  final account = await EmailAccount.db.findFirstRow(
    session,
    where: (final t) => t.email.equals(email),
    transaction: transaction,
  );

  if (account == null) {
    await _rateLimitUtil.recordAttempt(
      session,
      nonce: email,
    );
    throw EmailAccountNotFoundException();
  }

  if (!await _hashUtil.validateHashFromString(
    secret: password,
    hashString: account.passwordHash,
  )) {
    await _rateLimitUtil.recordAttempt(
      session,
      nonce: email,
    );
    throw EmailAuthenticationInvalidCredentialsException();
  }

  return account.authUserId;
}