completePasswordReset method

Future<UuidValue> completePasswordReset(
  1. Session session, {
  2. required String completePasswordResetToken,
  3. required String newPassword,
  4. required Transaction transaction,
})

Returns the auth user ID for the successfully changed password.

This method should only be called after the verifyPasswordResetCode method has been called successfully.

The method takes the completePasswordResetToken returned from verifyPasswordResetCode and uses it to complete the password reset.

Can throw the following EmailPasswordResetServerException subclasses:

Implementation

Future<UuidValue> completePasswordReset(
  final Session session, {
  required final String completePasswordResetToken,
  required final String newPassword,
  required final Transaction transaction,
}) async {
  if (!_config.passwordValidationFunction(newPassword)) {
    throw EmailPasswordResetPasswordPolicyViolationException();
  }

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

  await EmailAccountPasswordResetRequest.db.deleteRow(
    session,
    resetRequest,
    transaction: transaction,
  );

  final account = (await EmailAccount.db.findById(
    session,
    resetRequest.emailAccountId,
    transaction: transaction,
  ));
  if (account == null) {
    throw EmailPasswordResetEmailNotFoundException();
  }

  await setPassword(
    session,
    emailAccount: account,
    password: newPassword,
    transaction: transaction,
  );

  // Call the password reset completion callback
  _config.onPasswordResetCompleted?.call(
    session,
    emailAccountId: account.id!,
    transaction: transaction,
  );

  return account.authUserId;
}