-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Login action return user data + added
account/auth-check
route
- Loading branch information
Showing
6 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* UserFrosting Account Sprinkle (http://www.userfrosting.com) | ||
* | ||
* @link https://github.com/userfrosting/sprinkle-account | ||
* @copyright Copyright (c) 2013-2024 Alexander Weissman & Louis Charette | ||
* @license https://github.com/userfrosting/sprinkle-account/blob/master/LICENSE.md (MIT License) | ||
*/ | ||
|
||
namespace UserFrosting\Sprinkle\Account\Controller; | ||
|
||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator; | ||
|
||
/** | ||
* Return if the user is authenticated, and if he is, also return the user data. | ||
* | ||
* Middleware: None | ||
* Route: /account/authcheck | ||
* Route Name: account.authCheck | ||
* Request type: GET | ||
*/ | ||
class AuthCheckAction | ||
{ | ||
/** | ||
* Inject dependencies. | ||
*/ | ||
public function __construct( | ||
protected Authenticator $authenticator, | ||
) { | ||
} | ||
|
||
/** | ||
* Handle request and return data. | ||
* | ||
* @param Response $response | ||
*/ | ||
public function __invoke(Response $response): Response | ||
{ | ||
$auth = $this->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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* UserFrosting Account Sprinkle (http://www.userfrosting.com) | ||
* | ||
* @link https://github.com/userfrosting/sprinkle-account | ||
* @copyright Copyright (c) 2013-2024 Alexander Weissman & Louis Charette | ||
* @license https://github.com/userfrosting/sprinkle-account/blob/master/LICENSE.md (MIT License) | ||
*/ | ||
|
||
namespace UserFrosting\Sprinkle\Account\Tests\Controller; | ||
|
||
use Mockery; | ||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator; | ||
use UserFrosting\Sprinkle\Account\Database\Models\User; | ||
use UserFrosting\Sprinkle\Account\Tests\AccountTestCase; | ||
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase; | ||
|
||
/** | ||
* Tests RegisterAction | ||
*/ | ||
class AuthCheckActionTest extends AccountTestCase | ||
{ | ||
use RefreshDatabase; | ||
use MockeryPHPUnitIntegration; | ||
|
||
/** | ||
* Setup test database for controller tests | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters