completeAccountCreation static method

Future<({String email, UuidValue emailAccountId})> completeAccountCreation(
  1. Session session, {
  2. required UuidValue accountRequestId,
  3. required UuidValue authUserId,
  4. Transaction? transaction,
})

Finalize the email authentication creation.

Returns the ID of the new email authentication, and the email address used during registration.

Throws an EmailAccountRequestNotFoundException in case the accountRequestId does not point to an existing request. Throws an EmailAccountRequestNotVerifiedException in case the request has not been verified via verifyAccountCreation.

Implementation

static Future<({UuidValue emailAccountId, String email})>
    completeAccountCreation(
  final Session session, {
  required final UuidValue accountRequestId,

  /// Authentication user ID this account should be linked up with
  required final UuidValue authUserId,
  final Transaction? transaction,
}) async {
  return DatabaseUtil.runInTransactionOrSavepoint(
    session.db,
    transaction,
    (final transaction) async {
      final request = await EmailAccountRequest.db.findById(
        session,
        accountRequestId,
        transaction: transaction,
      );

      if (request == null) {
        throw EmailAccountRequestNotFoundException();
      }

      if (request.verifiedAt == null) {
        throw EmailAccountRequestNotVerifiedException();
      }

      await EmailAccountRequest.db.deleteRow(
        session,
        request,
        transaction: transaction,
      );

      final account = await EmailAccount.db.insertRow(
        session,
        EmailAccount(
          authUserId: authUserId,
          email: request.email,
          passwordHash: request.passwordHash,
          passwordSalt: request.passwordSalt,
        ),
        transaction: transaction,
      );

      return (emailAccountId: account.id!, email: request.email);
    },
  );
}