From 838c6edf66e05727a2617d41efdcb0fe40632a14 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 30 Jul 2017 14:19:07 +0200 Subject: [PATCH] Implement method 'confirmEmailAndSignIn' in class 'Auth' --- src/Auth.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/Auth.php b/src/Auth.php index 6debe62..485f2c2 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -529,6 +529,45 @@ final class Auth extends UserManager { } } + /** + * Confirms an email address and activates the account by supplying the correct selector/token pair + * + * The selector/token pair must have been generated previously by registering a new account + * + * The user will be automatically signed in if this operation is successful + * + * @param string $selector the selector from the selector/token pair + * @param string $token the token from the selector/token pair + * @param int|null $rememberDuration (optional) the duration in seconds to keep the user logged in ("remember me"), e.g. `60 * 60 * 24 * 365.25` for one year + * @return string the email address that has successfully been verified + * @throws InvalidSelectorTokenPairException if either the selector or the token was not correct + * @throws TokenExpiredException if the token has already expired + * @throws InvalidEmailException if the email address has been invalid + * @throws AuthError if an internal problem occurred (do *not* catch) + */ + public function confirmEmailAndSignIn($selector, $token, $rememberDuration = null) { + $verifiedEmail = $this->confirmEmail($selector, $token); + + if (!$this->isLoggedIn()) { + if ($verifiedEmail !== null) { + $verifiedEmail = self::validateEmailAddress($verifiedEmail); + + $userData = $this->getUserDataByEmailAddress( + $verifiedEmail, + [ 'id', 'email', 'username', 'status', 'roles_mask' ] + ); + + $this->onLoginSuccessful($userData['id'], $userData['email'], $userData['username'], $userData['status'], $userData['roles_mask'], true); + + if ($rememberDuration !== null) { + $this->createRememberDirective($userData['id'], $rememberDuration); + } + } + } + + return $verifiedEmail; + } + /** * Changes the (currently logged-in) user's password *