1
0
mirror of https://github.com/delight-im/PHP-Auth.git synced 2025-07-30 21:00:13 +02:00

Refactor validation of email addresses

This commit is contained in:
Marco
2016-08-20 17:05:47 +02:00
parent f45e0f1cb4
commit 9d2d764ced

View File

@@ -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")
*