diff --git a/backend/app/Domain/Usecases/AuthLogin/AuthLoginUseCase.php b/backend/app/Domain/Usecases/AuthLogin/AuthLoginUseCase.php index 3e9619b..da672eb 100644 --- a/backend/app/Domain/Usecases/AuthLogin/AuthLoginUseCase.php +++ b/backend/app/Domain/Usecases/AuthLogin/AuthLoginUseCase.php @@ -2,6 +2,7 @@ namespace App\Domain\Usecases\AuthLogin; +use Illuminate\Http\Response; use App\Exceptions\JsonException; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Validator; @@ -13,7 +14,7 @@ public function __construct() { } - public function handler(AuthLoginDto $dto): string + public function handler(AuthLoginDto $dto): Response { // mount credentials $credentials = ["username" => $dto->getUsername(), "password" => $dto->getPassword()]; @@ -43,7 +44,10 @@ public function handler(AuthLoginDto $dto): string ]); } - // return jwt token - return $token; + // return response with built-in token on cookie + $response = new Response(json_encode(auth()->user())); + return $response + ->withHeaders(['Cache-Control' => 'no-cache, private']) + ->withCookie(cookie("Authorization", $token, 60, null, null, false, true, false, null)); } } diff --git a/backend/app/Domain/Usecases/AuthRegister/AuthRegisterUseCase.php b/backend/app/Domain/Usecases/AuthRegister/AuthRegisterUseCase.php index 82bacaa..b4324dd 100644 --- a/backend/app/Domain/Usecases/AuthRegister/AuthRegisterUseCase.php +++ b/backend/app/Domain/Usecases/AuthRegister/AuthRegisterUseCase.php @@ -5,19 +5,22 @@ use App\Exceptions\JsonException; use Illuminate\Support\Facades\Validator; -use App\Domain\Entities\UserEntity; -use App\Domain\Repositories\ICreateUserRepository\ICreateUserDto; +use Illuminate\Http\Response; +use App\Domain\Usecases\AuthLogin\AuthLoginDto; +use App\Domain\Usecases\AuthLogin\AuthLoginUseCase; use App\Domain\Usecases\AuthRegister\AuthRegisterDto; +use App\Domain\Repositories\ICreateUserRepository\ICreateUserDto; use App\Domain\Repositories\ICreateUserRepository\ICreateUserRepository; - class AuthRegisterUseCase { - public function __construct(private readonly ICreateUserRepository $createUserRepository) - { + public function __construct( + private readonly ICreateUserRepository $createUserRepository, + private readonly AuthLoginUseCase $authLoginUsecase, + ) { } - public function handler(AuthRegisterDto $dto): UserEntity + public function handler(AuthRegisterDto $dto): Response { // mount payload $payload = [ @@ -43,12 +46,18 @@ public function handler(AuthRegisterDto $dto): UserEntity ]); } - // create a new user and return it - return $this->createUserRepository->handler(new ICreateUserDto( + // create a new user on database + $this->createUserRepository->handler(new ICreateUserDto( array_merge($payload, [ "role" => "customer", "balance" => 0 ]) )); + + // return authenticated user to response + return $this->authLoginUsecase->handler(new AuthLoginDto([ + "username" => $dto->getUsername(), + "password" => $dto->getPassword(), + ])); } } diff --git a/backend/app/Http/Controllers/AuthLoginController.php b/backend/app/Http/Controllers/AuthLoginController.php index b812f0b..2828342 100644 --- a/backend/app/Http/Controllers/AuthLoginController.php +++ b/backend/app/Http/Controllers/AuthLoginController.php @@ -4,7 +4,6 @@ use Exception; use Illuminate\Http\Request; -use Illuminate\Http\Response; use App\Http\Controllers\_Controller; use App\Domain\Usecases\AuthLogin\AuthLoginDto; use App\Domain\Usecases\AuthLogin\AuthLoginUseCase; @@ -21,14 +20,10 @@ public function __construct() public function handler(Request $request) { try { - $token = $this->authLoginUsecase->handler(new AuthLoginDto([ + return $this->authLoginUsecase->handler(new AuthLoginDto([ "username" => $request->input('username'), "password" => $request->input('password'), ])); - $response = new Response(json_encode(auth()->user())); - return $response - ->withHeaders(['Cache-Control' => 'no-cache, private']) - ->withCookie(cookie("Authorization", $token, 60, null, null, false, true, false, null)); } catch (Exception $error) { return response()->json(json_decode($error->getMessage()), 401); } diff --git a/backend/app/Http/Controllers/AuthRegisterController.php b/backend/app/Http/Controllers/AuthRegisterController.php index fcd9539..47b5c94 100644 --- a/backend/app/Http/Controllers/AuthRegisterController.php +++ b/backend/app/Http/Controllers/AuthRegisterController.php @@ -5,6 +5,7 @@ use Exception; use Illuminate\Http\Request; use App\Http\Controllers\_Controller; +use App\Domain\Usecases\AuthLogin\AuthLoginUseCase; use App\Domain\Usecases\AuthRegister\AuthRegisterDto; use App\Domain\Usecases\AuthRegister\AuthRegisterUseCase; use App\Infrastructure\Repositories\EloquentCreateUserRepository; @@ -15,21 +16,20 @@ class AuthRegisterController extends _Controller public function __construct() { $this->authRegisterUsecase = new AuthRegisterUseCase( - new EloquentCreateUserRepository() + new EloquentCreateUserRepository(), + new AuthLoginUseCase() ); } public function handler(Request $request) { try { - // execute registration usecase - $user = $this->authRegisterUsecase->handler(new AuthRegisterDto([ + // execute registration usecase and return authenticated + return $this->authRegisterUsecase->handler(new AuthRegisterDto([ "email" => $request->input('email'), "username" => $request->input('username'), "password" => $request->input('password'), ])); - // return created user - return response()->json($user->toJson()); } catch (Exception $error) { // return error message if something happens return response()->json(json_decode($error->getMessage()), 400);