createAccountRequest static method
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 {
if (AuthConfig.current.sendValidationEmail == null) {
session.log(
'SendValidationEmail is not configured, cannot send email.',
level: LogLevel.debug,
);
return false;
}
try {
// Check if user already has an account
var userInfo = await Users.findUserByEmail(session, email);
if (userInfo != null) {
session.log(
"Email: '$email' already taken!",
level: LogLevel.debug,
);
return false;
}
email = email.trim().toLowerCase();
if (!EmailValidator.validate(email)) {
session.log(
"Email: '$email' is not valid!",
level: LogLevel.debug,
);
return false;
}
userName = userName.trim();
if (userName.isEmpty) {
session.log(
'Invalid userName!'
"'userName' must not be empty.",
level: LogLevel.debug,
);
return false;
}
if (password.length < AuthConfig.current.minPasswordLength ||
password.length > AuthConfig.current.maxPasswordLength) {
session.log(
'Invalid password!\n'
'Password length must be >= ${AuthConfig.current.minPasswordLength}'
' and '
'<= ${AuthConfig.current.maxPasswordLength}',
level: LogLevel.debug,
);
return false;
}
var accountRequest = await findAccountRequest(session, email);
if (accountRequest == null) {
accountRequest = EmailCreateAccountRequest(
userName: userName,
email: email,
hash: await generatePasswordHash(password),
verificationCode: _generateVerificationCode(),
);
await EmailCreateAccountRequest.db.insertRow(session, accountRequest);
} else {
accountRequest.userName = userName;
accountRequest.verificationCode = _generateVerificationCode();
await EmailCreateAccountRequest.db.updateRow(session, accountRequest);
}
return await AuthConfig.current.sendValidationEmail!(
session,
email,
accountRequest.verificationCode,
);
} catch (e) {
session.log(
'$e',
level: LogLevel.debug,
);
return false;
}
}