From ee4371125f874462c415b673fc3a8a5961f4011a Mon Sep 17 00:00:00 2001 From: Dave Earley Date: Thu, 29 Aug 2024 22:18:13 -0700 Subject: [PATCH] Fix multi-account login --- backend/app/Http/Actions/Auth/BaseAuthAction.php | 10 +++++++--- .../Resources/Auth/AuthenticatedResponseResource.php | 4 ++-- ...tedResponseDTO.php => AuthenticatedResponseDTO.php} | 2 +- frontend/src/components/routes/auth/Login/index.tsx | 6 +++--- 4 files changed, 13 insertions(+), 9 deletions(-) rename backend/app/Services/Handlers/Auth/DTO/{AuthenicatedResponseDTO.php => AuthenticatedResponseDTO.php} (89%) diff --git a/backend/app/Http/Actions/Auth/BaseAuthAction.php b/backend/app/Http/Actions/Auth/BaseAuthAction.php index be2a23cf..d11699a1 100644 --- a/backend/app/Http/Actions/Auth/BaseAuthAction.php +++ b/backend/app/Http/Actions/Auth/BaseAuthAction.php @@ -4,7 +4,7 @@ use HiEvents\Http\Actions\BaseAction; use HiEvents\Resources\Auth\AuthenticatedResponseResource; -use HiEvents\Services\Handlers\Auth\DTO\AuthenicatedResponseDTO; +use HiEvents\Services\Handlers\Auth\DTO\AuthenticatedResponseDTO; use Illuminate\Http\JsonResponse; use Illuminate\Http\Response; use Illuminate\Support\Collection; @@ -23,8 +23,12 @@ protected function getAuthCookie(string $token): SymfonyCookie ); } - protected function addTokenToResponse(JsonResponse|Response $response, string $token): JsonResponse + protected function addTokenToResponse(JsonResponse|Response $response, ?string $token): JsonResponse { + if (!$token) { + return $response; + } + $response = $response->withCookie($this->getAuthCookie($token)); $response->header('X-Auth-Token', $token); @@ -37,7 +41,7 @@ protected function respondWithToken(?string $token, Collection $accounts): JsonR $user = $this->getAuthenticatedUser(); return $this->addTokenToResponse( - response: $this->jsonResponse(new AuthenticatedResponseResource(new AuthenicatedResponseDTO( + response: $this->jsonResponse(new AuthenticatedResponseResource(new AuthenticatedResponseDTO( token: $token, expiresIn: auth()->factory()->getTTL() * 60, accounts: $accounts, diff --git a/backend/app/Resources/Auth/AuthenticatedResponseResource.php b/backend/app/Resources/Auth/AuthenticatedResponseResource.php index 480b6508..f1603441 100644 --- a/backend/app/Resources/Auth/AuthenticatedResponseResource.php +++ b/backend/app/Resources/Auth/AuthenticatedResponseResource.php @@ -4,11 +4,11 @@ use HiEvents\Resources\Account\AccountResource; use HiEvents\Resources\User\UserResource; -use HiEvents\Services\Handlers\Auth\DTO\AuthenicatedResponseDTO; +use HiEvents\Services\Handlers\Auth\DTO\AuthenticatedResponseDTO; use Illuminate\Http\Resources\Json\JsonResource; /** - * @mixin AuthenicatedResponseDTO + * @mixin AuthenticatedResponseDTO */ class AuthenticatedResponseResource extends JsonResource { diff --git a/backend/app/Services/Handlers/Auth/DTO/AuthenicatedResponseDTO.php b/backend/app/Services/Handlers/Auth/DTO/AuthenticatedResponseDTO.php similarity index 89% rename from backend/app/Services/Handlers/Auth/DTO/AuthenicatedResponseDTO.php rename to backend/app/Services/Handlers/Auth/DTO/AuthenticatedResponseDTO.php index d3d24053..45987890 100644 --- a/backend/app/Services/Handlers/Auth/DTO/AuthenicatedResponseDTO.php +++ b/backend/app/Services/Handlers/Auth/DTO/AuthenticatedResponseDTO.php @@ -6,7 +6,7 @@ use HiEvents\DomainObjects\UserDomainObject; use Illuminate\Support\Collection; -class AuthenicatedResponseDTO extends BaseDTO +class AuthenticatedResponseDTO extends BaseDTO { public function __construct( public ?string $token, diff --git a/frontend/src/components/routes/auth/Login/index.tsx b/frontend/src/components/routes/auth/Login/index.tsx index 2827c5ea..c193369d 100644 --- a/frontend/src/components/routes/auth/Login/index.tsx +++ b/frontend/src/components/routes/auth/Login/index.tsx @@ -22,7 +22,7 @@ const Login = () => { }); const [showChooseAccount, setShowChooseAccount] = useState(false); - const {mutate: loginUser, isLoading, data} = useMutation({ + const {mutate: loginUser, isPending, data} = useMutation({ mutationFn: (userData: LoginData) => authClient.login(userData), onSuccess: (response: LoginResponse) => { @@ -62,8 +62,8 @@ const Login = () => { {t`Forgot password?`}

-