[new IsEmailRule()], "password" => [new HasLengthRule(UserManager::MIN_PASSWORD_LENGTH, UserManager::MAX_PASSWORD_LENGTH)], ], ); $this->conn = $conn; $this->user_manager = $user_manager; $this->mail_manager = $mail_manager; } /** * Registers a new user. * * @param array $inputs `"email": string`: the email address to register, `"password": string`: * the password to use for the new account * @return null * @throws InvalidInputException if the email address is already used */ function handle(array $inputs): mixed { Database::transaction($this->conn, function () use ($inputs) { if ($this->user_manager->has_user_with_email($inputs["email"])) throw new InvalidInputException("That email address already in use by someone else.", "email"); $uuid = $this->user_manager->add_user($inputs["email"], $inputs["password"]); $user_data = $this->user_manager->get_user_by_uuid($uuid); if ($user_data === null) throw new ActionException("Failed to retrieve account data. Refresh the page and try again."); $token = $user_data["email_verification_token"]; $this->mail_manager->queue_email(new RegisterEmail($inputs["email"], $token)); }); return null; } } /** * An email to be sent to a recently registered user, including instructions for email verification. */ class RegisterEmail extends Email { /** * A string identifying the type of email. */ public const TYPE = "register"; /** * @var string the token to verify the email address with */ public string $token; /** * Constructs a new `RegisterEmail`. * * @param string $recipient the intended recipient of the email * @param string $token the token to verify the email address with */ public function __construct(string $recipient, string $token) { parent::__construct($this::TYPE, $recipient); $this->token = $token; } public function get_subject(): string { return "Welcome to Death Notifier"; } public function get_body(): string { $base_path = Config::get()["server"]["base_path"]; $verify_path = "$base_path?action=verify-email&email=" . rawurlencode($this->recipient) . "&token=$this->token"; return "You have successfully created an account with Death Notifier. " . "Welcome!" . "\n\n" . "Until you verify your email address, you will not receive any notifications. " . "You can verify your email address by clicking the link below. " . "This link will expire after " . UserManager::MINUTES_VALID_VERIFICATION . " minutes." . "\n" . "Verify: $verify_path" . "\n\n" . "If you did not create this account, you can delete the account. " . "Go to the Death Notifier website, reset your password, log in, and delete the account." . "\n\n" . $base_path; } }