Skip to content

Commit cfd3bb7

Browse files
chore: Add PR's requested changes
1 parent 9fc38a0 commit cfd3bb7

1 file changed

Lines changed: 13 additions & 30 deletions

File tree

app/libs/Auth/AuthService.php

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Auth\Exceptions\AuthenticationException;
2020
use Auth\Exceptions\AuthenticationLockedUserLoginAttempt;
2121
use Auth\Repositories\IUserRepository;
22+
use Illuminate\Contracts\Auth\Authenticatable;
2223
use Illuminate\Support\Facades\Auth;
2324
use Illuminate\Support\Facades\Config;
2425
use Illuminate\Support\Facades\Crypt;
@@ -134,11 +135,7 @@ public function isUserLogged()
134135
*/
135136
public function getCurrentUser(): ?User
136137
{
137-
$user = Auth::user();
138-
if ($user instanceof User) {
139-
return $user;
140-
}
141-
return null;
138+
return Auth::user();
142139
}
143140

144141
/**
@@ -152,10 +149,11 @@ public function login(string $username, string $password, bool $remember_me): bo
152149
{
153150
Log::debug("AuthService::login");
154151

152+
$this->last_login_error = "";
155153
if (!Auth::attempt(['username' => $username, 'password' => $password], $remember_me)) {
156154
throw new AuthenticationException
157155
(
158-
"username or password does not match an existing record."
156+
"We are sorry, your username or password does not match an existing record."
159157
);
160158
}
161159
Log::debug("AuthService::login: clearing principal");
@@ -164,7 +162,7 @@ public function login(string $username, string $password, bool $remember_me): bo
164162
if (is_null($current_user) || !$current_user->canLogin())
165163
throw new AuthenticationException
166164
(
167-
"username or password does not match an existing record."
165+
"We are sorry, your username or password does not match an existing record."
168166
);
169167
$this->principal_service->register
170168
(
@@ -185,28 +183,13 @@ public function validateCredentials(string $username, string $password): User
185183
{
186184
Log::debug("AuthService::validateCredentials");
187185

188-
// retrieveByCredentials swallows AuthenticationLockedUserLoginAttempt and returns null,
189-
// so pre-check lock state here to surface a distinct message for locked accounts.
190-
$existing = $this->user_repository->getByEmailOrName($username);
191-
if (!is_null($existing) && !$existing->isActive()) {
192-
throw new AuthenticationException(
193-
sprintf("User %s is locked.", $username)
194-
);
195-
}
196-
197-
// Known cost: retrieveByCredentials() calls user_repository->getByEmailOrName() internally
198-
// (CustomAuthProvider line ~122), duplicating the query above. Eliminating it would require
199-
// either changing the provider API to accept a pre-fetched User, or moving
200-
// LockUserCounterMeasure checkpoint logic out of the provider — both out of scope here.
201-
$user = Auth::getProvider()->retrieveByCredentials([
202-
'username' => $username,
203-
'password' => $password,
204-
]);
205-
206-
if (is_null($user) || !$user instanceof User || !$user->canLogin()) {
207-
throw new AuthenticationException(
208-
"username or password does not match an existing record."
209-
);
186+
/**
187+
* @var User|null $user
188+
*/
189+
$user = $this->user_repository->getByEmailOrName($username);
190+
$valid = Auth::getProvider()->validateCredentials($user, ['username' => $username, 'password' => $password]);
191+
if (!$valid) {
192+
throw new AuthenticationException();
210193
}
211194

212195
return $user;
@@ -315,7 +298,7 @@ public function loginWithOTP(OAuth2OTP $otpClaim, ?Client $client = null, bool $
315298

316299
if (!$user->canLogin()) {
317300
Log::warning(sprintf("AuthService::loginWithOTP user %s cannot login ( is not active ).", $user->getId()));
318-
throw new AuthenticationException("username or password does not match an existing record.");
301+
throw new AuthenticationException("We are sorry, your username or password does not match an existing record.");
319302
}
320303

321304
$otp->setAuthTime(time());

0 commit comments

Comments
 (0)