From 421a0e43b6fd46e5ac65dbba51e73b2b0212ca9e Mon Sep 17 00:00:00 2001 From: Louis Charette Date: Sat, 6 Jul 2024 13:24:03 -0400 Subject: [PATCH] Login action return user data + added `account/auth-check` route --- CHANGELOG.md | 4 + app/src/Controller/AuthCheckAction.php | 54 ++++++++++++++ app/src/Controller/LoginAction.php | 19 ++++- app/src/Routes/AuthRoutes.php | 2 + app/tests/Controller/AuthCheckActionTest.php | 78 ++++++++++++++++++++ app/tests/Controller/LoginActionTest.php | 9 ++- 6 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 app/src/Controller/AuthCheckAction.php create mode 100644 app/tests/Controller/AuthCheckActionTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index cd09457..8c19113 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [6.0.0](https://github.com/userfrosting/sprinkle-account/compare/5.2.0...6.0.0) +- Login action returns the user data instead of empty array +- Added `account/auth-check` route + ## [5.2.0](https://github.com/userfrosting/sprinkle-account/compare/5.1.0...5.2.0) ## [5.1.3](https://github.com/userfrosting/sprinkle-account/compare/5.1.2...5.1.3) diff --git a/app/src/Controller/AuthCheckAction.php b/app/src/Controller/AuthCheckAction.php new file mode 100644 index 0000000..7a514e9 --- /dev/null +++ b/app/src/Controller/AuthCheckAction.php @@ -0,0 +1,54 @@ +authenticator->check(); + $data = [ + 'auth' => $auth, + 'user' => $auth ? $this->authenticator->user() : null, + ]; + $payload = json_encode($data, JSON_THROW_ON_ERROR); + $response->getBody()->write($payload); + + return $response->withHeader('Content-Type', 'application/json'); + } +} diff --git a/app/src/Controller/LoginAction.php b/app/src/Controller/LoginAction.php index df334b4..66f0925 100644 --- a/app/src/Controller/LoginAction.php +++ b/app/src/Controller/LoginAction.php @@ -77,18 +77,31 @@ public function __construct( public function __invoke(Request $request, Response $response): Response { $this->handle($request); + $response = $this->writeResponse($response); + return $response->withHeader('Content-Type', 'application/json'); + } + + /** + * Write to the response object. + * + * @param Response $response + * + * @return Response + */ + protected function writeResponse(Response $response): Response + { // Get redirect target and add Header $event = $this->eventDispatcher->dispatch(new UserRedirectedAfterLoginEvent()); if ($event->getRedirect() !== null) { $response = $response->withHeader('UF-Redirect', $event->getRedirect()); } - // Write empty response - $payload = json_encode([], JSON_THROW_ON_ERROR); + // Write response with the user info in it + $payload = json_encode($this->authenticator->user(), JSON_THROW_ON_ERROR); $response->getBody()->write($payload); - return $response->withHeader('Content-Type', 'application/json'); + return $response; } /** diff --git a/app/src/Routes/AuthRoutes.php b/app/src/Routes/AuthRoutes.php index b4cee49..3e8b8ba 100644 --- a/app/src/Routes/AuthRoutes.php +++ b/app/src/Routes/AuthRoutes.php @@ -17,6 +17,7 @@ use UserFrosting\Routes\RouteDefinitionInterface; use UserFrosting\Sprinkle\Account\Authenticate\AuthGuard; use UserFrosting\Sprinkle\Account\Authenticate\GuestGuard; +use UserFrosting\Sprinkle\Account\Controller\AuthCheckAction; use UserFrosting\Sprinkle\Account\Controller\CaptchaAction; use UserFrosting\Sprinkle\Account\Controller\CheckUsernameAction; use UserFrosting\Sprinkle\Account\Controller\DenyResetPasswordAction; @@ -56,6 +57,7 @@ public function register(App $app): void // No guard $app->group('/account', function (RouteCollectorProxy $group) { + $group->get('/auth-check', AuthCheckAction::class)->setName('account.authCheck'); $group->get('/captcha', CaptchaAction::class)->setName('account.captcha'); $group->get('/check-username', CheckUsernameAction::class)->setName('account.checkUsername'); $group->get('/suggest-username', SuggestUsernameAction::class)->setName('account.suggestUsername'); diff --git a/app/tests/Controller/AuthCheckActionTest.php b/app/tests/Controller/AuthCheckActionTest.php new file mode 100644 index 0000000..da96f97 --- /dev/null +++ b/app/tests/Controller/AuthCheckActionTest.php @@ -0,0 +1,78 @@ +refreshDatabase(); + } + + public function testGuest(): void + { + // Create request with method and url and fetch response + $request = $this->createJsonRequest('GET', '/account/auth-check'); + $response = $this->handleRequest($request); + + // Assert response status & body + $this->assertJsonResponse([ + 'auth' => false, + 'user' => null, + ], $response); + $this->assertResponseStatus(200, $response); + } + + public function testNotAuth(): void + { + /** @var User */ + $user = User::factory([ + 'password' => 'test' + ])->create(); + + // Mock Authenticator + $authenticator = Mockery::mock(Authenticator::class) + ->shouldReceive('check')->once()->andReturn(true) + ->shouldReceive('user')->once()->andReturn($user) + ->getMock(); + $this->ci->set(Authenticator::class, $authenticator); + + // Create request with method and url and fetch response + $request = $this->createJsonRequest('GET', '/account/auth-check'); + $response = $this->handleRequest($request); + + // Assert response status & body + $this->assertJsonResponse([ + 'auth' => true, + 'user' => $user->toArray(), + ], $response); + $this->assertResponseStatus(200, $response); + } +} diff --git a/app/tests/Controller/LoginActionTest.php b/app/tests/Controller/LoginActionTest.php index abb311e..36b7ace 100644 --- a/app/tests/Controller/LoginActionTest.php +++ b/app/tests/Controller/LoginActionTest.php @@ -49,6 +49,7 @@ public function testLogin(): void $user = User::factory([ 'password' => 'test' ])->create(); + $user->refresh(); // Create request with method and url and fetch response $request = $this->createJsonRequest('POST', '/account/login', [ @@ -58,7 +59,7 @@ public function testLogin(): void $response = $this->handleRequest($request); // Assert response status & body - $this->assertJsonResponse([], $response); + $this->assertJsonResponse($user->toArray(), $response); $this->assertResponseStatus(200, $response); // Assert Event Redirect @@ -82,6 +83,7 @@ public function testLoginWithEmail(): void $user = User::factory([ 'password' => 'test' ])->create(); + $user->refresh(); // Create request with method and url and fetch response $request = $this->createJsonRequest('POST', '/account/login', [ @@ -91,7 +93,7 @@ public function testLoginWithEmail(): void $response = $this->handleRequest($request); // Assert response status & body - $this->assertJsonResponse([], $response); + $this->assertJsonResponse($user->toArray(), $response); $this->assertResponseStatus(200, $response); // We have to logout the user to avoid problem @@ -195,6 +197,7 @@ public function testLoginThrottlerDoesNotCountSuccessfulLogins(): void $user = User::factory([ 'password' => 'test' ])->create(); + $user->refresh(); // Create fake throttler /** @var Throttler */ @@ -212,7 +215,7 @@ public function testLoginThrottlerDoesNotCountSuccessfulLogins(): void $response = $this->handleRequest($request); // Assert response status & body - $this->assertJsonResponse([], $response); + $this->assertJsonResponse($user->toArray(), $response); $this->assertResponseStatus(200, $response); // We have to logout the user to avoid problem