completeAccountCreation method

Future<EmailIdpCompleteAccountCreationResult> completeAccountCreation(
  1. Session session, {
  2. required String completeAccountCreationToken,
  3. required String password,
  4. required Transaction transaction,
})

The last step in the account creation process.

Given a token returned by verifyRegistrationCode, this method will complete the account creation process by creating a new authentication user and linking the account request to it.

Can throw the following EmailAccountRequestServerException subclasses:

Returns the result of the operation with the ID of the new authentication user, the account request ID and the email address used during registration.

The account request will be deleted after successful completion.

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

Implementation

Future<EmailIdpCompleteAccountCreationResult> completeAccountCreation(
  final Session session, {
  required final String completeAccountCreationToken,
  required final String password,
  required final Transaction transaction,
}) async {
  if (!_config.passwordValidationFunction(password)) {
    throw EmailPasswordPolicyViolationException();
  }

  final request = await withReplacedSecretChallengeException(
    () => _challengeUtil.completeChallenge(
      session,
      completionToken: completeAccountCreationToken,
      transaction: transaction,
    ),
  );

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

  final newUser = await _authUsers.create(
    session,
    transaction: transaction,
  );

  final passwordHash = await _hashUtils.createHashFromString(
    secret: password,
  );

  final emailAccount = await EmailAccount.db.insertRow(
    session,
    EmailAccount(
      authUserId: newUser.id,
      email: request.email,
      passwordHash: passwordHash,
    ),
    transaction: transaction,
  );

  await _config.onAfterAccountCreated?.call(
    session,
    email: emailAccount.email,
    authUserId: emailAccount.authUserId,
    emailAccountId: emailAccount.id!,
    transaction: transaction,
  );

  return EmailIdpCompleteAccountCreationResult._(
    authUserId: newUser.id,
    accountRequestId: request.id!,
    email: request.email,
    scopes: newUser.scopes,
  );
}