From 9d2d764ced272168292fbd421eda01a0c12a8fd3 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 20 Aug 2016 17:05:47 +0200 Subject: [PATCH] Refactor validation of email addresses --- src/Auth.php | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Auth.php b/src/Auth.php index 63c76de..f14b1a0 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -144,13 +144,7 @@ class Auth { public function register($email, $password, $username = null, callable $emailConfirmationCallback = null) { $this->throttle(self::THROTTLE_ACTION_REGISTER); - $email = isset($email) ? trim($email) : null; - if (empty($email)) { - throw new InvalidEmailException(); - } - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { - throw new InvalidEmailException(); - } + $email = self::validateEmailAddress($email); $password = isset($password) ? trim($password) : null; if (empty($password)) { @@ -256,13 +250,7 @@ class Auth { * @throws AuthError if an internal problem occurred (do *not* catch) */ public function login($email, $password, $remember = false) { - $email = isset($email) ? trim($email) : null; - if (empty($email)) { - throw new InvalidEmailException(); - } - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { - throw new InvalidEmailException(); - } + $email = self::validateEmailAddress($email); $password = isset($password) ? trim($password) : null; if (empty($password)) { @@ -313,6 +301,27 @@ class Auth { } } + /** + * Validates an email address + * + * @param string $email the email address to validate + * @return string the email address if it's valid + * @throws InvalidEmailException if the email address was invalid + */ + private static function validateEmailAddress($email) { + if (empty($email)) { + throw new InvalidEmailException(); + } + + $email = trim($email); + + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + throw new InvalidEmailException(); + } + + return $email; + } + /** * Creates a new directive keeping the user logged in ("remember me") *