Skip to content

Commit f4fd63d

Browse files
chore: Add PR's requested changes
Add tests changes with suggestion
1 parent 5b8e7a5 commit f4fd63d

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

app/libs/Auth/AuthService.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use App\Services\Auth\IUserService as IAuthUserService;
2020
use Auth\Exceptions\AuthenticationException;
2121
use Auth\Exceptions\AuthenticationLockedUserLoginAttempt;
22+
use Auth\Exceptions\UnverifiedEmailMemberException;
2223
use Auth\Repositories\IUserRepository;
2324
use Exception;
2425
use Illuminate\Support\Facades\Auth;
@@ -429,12 +430,16 @@ public function validateCredentials(string $username, string $password): User
429430
{
430431
Log::debug("AuthService::validateCredentials");
431432

432-
/**
433-
* @var User|null $user
434-
*/
435-
$user = Auth::getProvider()->retrieveByCredentials(['username' => $username, 'password' => $password]);
436-
if (!$user) {
437-
throw new AuthenticationException();
433+
try {
434+
/**
435+
* @var User|null $user
436+
*/
437+
$user = Auth::getProvider()->retrieveByCredentials(['username' => $username, 'password' => $password]);
438+
if (!$user instanceof User || !$user->canLogin()) {
439+
throw new AuthenticationException("We are sorry, your username or password does not match an existing record.");
440+
}
441+
} catch (UnverifiedEmailMemberException $ex) {
442+
throw new AuthenticationException($ex->getMessage());
438443
}
439444

440445
return $user;

tests/unit/AuthServiceValidateCredentialsTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Auth\AuthService;
1717
use Auth\CustomAuthProvider;
1818
use Auth\Exceptions\AuthenticationException;
19+
use Auth\Exceptions\UnverifiedEmailMemberException;
1920
use Auth\Repositories\IUserRepository;
2021
use Mockery;
2122
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
@@ -91,6 +92,7 @@ public function testValidCredentials_returnsUser_withoutEstablishingSession(): v
9192
$password = 'Str0ng!Pass';
9293

9394
$resolved_user = Mockery::mock('Auth\User');
95+
$resolved_user->shouldReceive('canLogin')->once()->andReturn(true);
9496

9597
$provider_mock = Mockery::mock(CustomAuthProvider::class);
9698
$provider_mock->shouldReceive('retrieveByCredentials')
@@ -179,4 +181,54 @@ public function testLoginUser_throwsException_whenIsNotActive(): void
179181
$this->service->loginUser($user, true);
180182
}
181183

184+
/**
185+
* UnverifiedEmailMemberException from the provider must be caught and
186+
* re-thrown as AuthenticationException (contract: @throws AuthenticationException only).
187+
*/
188+
public function testUnverifiedUser_throwsAuthenticationException(): void
189+
{
190+
$username = 'unverified@example.com';
191+
$password = 'any';
192+
193+
$provider_mock = Mockery::mock(CustomAuthProvider::class);
194+
$provider_mock->shouldReceive('retrieveByCredentials')
195+
->once()
196+
->with(['username' => $username, 'password' => $password])
197+
->andThrow(new UnverifiedEmailMemberException('Email not verified.'));
198+
199+
$this->auth_mock->shouldReceive('getProvider')->once()->andReturn($provider_mock);
200+
$this->auth_mock->shouldNotReceive('login');
201+
202+
$this->expectException(AuthenticationException::class);
203+
$this->expectExceptionMessage('Email not verified.');
204+
205+
$this->service->validateCredentials($username, $password);
206+
}
207+
208+
/**
209+
* Provider returns a valid User but canLogin() is false (locked/inactive):
210+
* must throw AuthenticationException — not silently return the user.
211+
*/
212+
public function testUserCannotLogin_throwsAuthenticationException(): void
213+
{
214+
$username = 'locked@example.com';
215+
$password = 'any';
216+
217+
$locked_user = Mockery::mock('Auth\User');
218+
$locked_user->shouldReceive('canLogin')->once()->andReturn(false);
219+
220+
$provider_mock = Mockery::mock(CustomAuthProvider::class);
221+
$provider_mock->shouldReceive('retrieveByCredentials')
222+
->once()
223+
->with(['username' => $username, 'password' => $password])
224+
->andReturn($locked_user);
225+
226+
$this->auth_mock->shouldReceive('getProvider')->once()->andReturn($provider_mock);
227+
$this->auth_mock->shouldNotReceive('login');
228+
229+
$this->expectException(AuthenticationException::class);
230+
231+
$this->service->validateCredentials($username, $password);
232+
}
233+
182234
}

0 commit comments

Comments
 (0)