Skip to content

Commit 4180cab

Browse files
committed
refactor: add LoginController::getEmailRules() and use it
1 parent cf8d1db commit 4180cab

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/Controllers/LoginController.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function getValidationRules(): array
6565
{
6666
return setting('Validation.login') ?? [
6767
//'username' => setting('Validation._username') ?? $this->getUsernameRules(),
68-
'email' => 'required|max_length[254]|valid_email',
68+
'email' => $this->getEmailRules(),
6969
'password' => 'required',
7070
];
7171
}
@@ -85,6 +85,20 @@ public static function getUsernameRules(): array
8585
];
8686
}
8787

88+
/**
89+
* Returns the validation rules for email
90+
*
91+
* @return string[]
92+
*/
93+
public static function getEmailRules(): array
94+
{
95+
return [
96+
'required',
97+
'max_length[254]',
98+
'valid_email',
99+
];
100+
}
101+
88102
/**
89103
* Logs the current user out.
90104
*/

src/Controllers/MagicLinkController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ private function recordLoginAttempt(
186186
/**
187187
* Returns the rules that should be used for validation.
188188
*
189-
* @return array<string, string>
189+
* @return array<string, array<string, string>>
190190
*/
191191
protected function getValidationRules(): array
192192
{
193193
return [
194-
'email' => 'required|max_length[254]|valid_email',
194+
'email' => LoginController::getEmailRules(),
195195
];
196196
}
197197
}

src/Controllers/RegisterController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,14 @@ protected function getValidationRules(): array
136136
$usernameRules,
137137
['is_unique[users.username]']
138138
);
139+
$registrationEmailRules = array_merge(
140+
LoginController::getEmailRules(),
141+
['is_unique[auth_identities.secret]']
142+
);
139143

140144
return setting('Validation.registration') ?? [
141145
'username' => $registrationUsernameRules,
142-
'email' => 'required|valid_email|is_unique[auth_identities.secret]',
146+
'email' => $registrationEmailRules,
143147
'password' => 'required|strong_password',
144148
'password_confirm' => 'required|matches[password]',
145149
];

0 commit comments

Comments
 (0)