createEmailAuthentication method

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

Creates an email authentication for the auth user with the given email and password.

The email will be treated as validated right away, so the caller must ensure that it comes from a trusted source. The password argument is not checked against the configured password policy. A null password can be passed to create an account without a password. In that case either the user has to complete a password reset or setPassword needs to be called before the user can log in.

Returns the email account ID for the newly created authentication method.

Implementation

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

  final passwordHash = password != null
      ? await EmailAccountSecretHash.createHash(value: password)
      : (hash: Uint8List.fromList([]), salt: Uint8List.fromList([]));

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

  return account.id!;
}