createAccountRequest static method

Future<bool> createAccountRequest(
  1. Session session,
  2. String userName,
  3. String email,
  4. String password,
)

Creates a request for creating an account associated with the specified email address. An email with a validation code will be sent.

Implementation

static Future<bool> createAccountRequest(
  Session session,
  String userName,
  String email,
  String password,
) async {
  assert(
    AuthConfig.current.sendValidationEmail != null,
    'The sendValidationEmail property needs to be set in AuthConfig.',
  );

  try {
    // Check if user already has an account
    var userInfo = await Users.findUserByEmail(session, email);
    if (userInfo != null) {
      return false;
    }

    email = email.trim().toLowerCase();
    if (!EmailValidator.validate(email)) {
      return false;
    }

    userName = userName.trim();
    if (userName.isEmpty) {
      return false;
    }

    if (password.length < 8 || password.length > 32) {
      return false;
    }

    var accountRequest = await findAccountRequest(session, email);
    if (accountRequest == null) {
      accountRequest = EmailCreateAccountRequest(
        userName: userName,
        email: email,
        hash: generatePasswordHash(password, email),
        verificationCode: _generateVerificationCode(),
      );
      await session.db.insert(accountRequest);
    } else {
      accountRequest.userName = userName;
      accountRequest.verificationCode = _generateVerificationCode();
      await EmailCreateAccountRequest.update(session, accountRequest);
    }

    return await AuthConfig.current.sendValidationEmail!(
      session,
      email,
      accountRequest.verificationCode,
    );
  } catch (e) {
    return false;
  }
}