Skip to content

Commit

Permalink
Return user object on login
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Sep 17, 2023
1 parent 979d7b9 commit f8e92d6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/assets/stores/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export const useAuthStore = defineStore("auth", {
this.error = null;
axios
.post("/auth/login", form)
.then(() => {
this.check();
.then((response) => {
this.user = response.data;
this.check(); // Check to make sure it worked
})
.catch((error) => {
this.error = error.response.data;
Expand All @@ -35,7 +36,7 @@ export const useAuthStore = defineStore("auth", {
.get("/auth/logout")
.then(() => {
this.user = null;
this.check();
this.check(); // Check to make sure it worked
})
.finally(() => {
this.loading = false;
Expand Down
36 changes: 36 additions & 0 deletions app/src/Controller/LoginAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* UserFrosting Vue Demo (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/demo-vue
* @copyright Copyright (c) 2023 Louis Charette
* @license https://github.com/userfrosting/demo-vue/blob/main/LICENSE.md (MIT License)
*/

namespace UserFrosting\Demo\Controller;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Sprinkle\Account\Controller\LoginAction as AccountLoginAction;

class LoginAction extends AccountLoginAction
{
/**
* Receive the request, dispatch to the handler, and return the payload to
* the response.
*
* @param Request $request
* @param Response $response
*/
public function __invoke(Request $request, Response $response): Response
{
$this->handle($request);

// Write response
$payload = json_encode($this->authenticator->user(), JSON_THROW_ON_ERROR);
$response->getBody()->write($payload);

return $response->withHeader('Content-Type', 'application/json');
}
}
3 changes: 2 additions & 1 deletion app/src/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

use Slim\App;
use UserFrosting\Demo\Controller\AppController;
use UserFrosting\Demo\Controller\LoginAction;
use UserFrosting\Routes\RouteDefinitionInterface;
use UserFrosting\Sprinkle\Account\Controller\LoginAction;

use UserFrosting\Sprinkle\Account\Controller\LogoutAction;

class Routes implements RouteDefinitionInterface
Expand Down
59 changes: 59 additions & 0 deletions app/tests/Controller/LoginActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* UserFrosting Vue Demo (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/demo-vue
* @copyright Copyright (c) 2023 Louis Charette
* @license https://github.com/userfrosting/demo-vue/blob/main/LICENSE.md (MIT License)
*/

namespace UserFrosting\Tests\Demo\Controller;

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use UserFrosting\Demo\Demo;
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator;
use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase;
use UserFrosting\Testing\TestCase;

class LoginActionTest extends TestCase
{
use RefreshDatabase;
use MockeryPHPUnitIntegration;

protected string $mainSprinkle = Demo::class;

/**
* Setup test database for controller tests
*/
public function setUp(): void
{
parent::setUp();
$this->refreshDatabase();
}

public function testLogin(): void
{
/** @var User */
$user = User::factory([
'password' => 'test'
])->create();

// Create request with method and url and fetch response
$request = $this->createJsonRequest('POST', '/auth/login', [
'user_name' => $user->user_name,
'password' => 'test',
]);
$response = $this->handleRequest($request);

// Assert response status & body
$this->assertResponseStatus(200, $response);
$this->assertJsonResponse($user->refresh()->toArray(), $response);

// We have to logout the user to avoid problem
/** @var Authenticator */
$authenticator = $this->ci->get(Authenticator::class);
$authenticator->logout();
}
}

0 comments on commit f8e92d6

Please sign in to comment.