Skip to content

Commit

Permalink
Merge pull request #74 from andreciornavei/develop
Browse files Browse the repository at this point in the history
adjusted registration usecase to return authenticated user on response
  • Loading branch information
andreciornavei authored Mar 8, 2024
2 parents 3db9bc1 + b6ad5a3 commit 57c140c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
10 changes: 7 additions & 3 deletions backend/app/Domain/Usecases/AuthLogin/AuthLoginUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()];
Expand Down Expand Up @@ -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));
}
}
25 changes: 17 additions & 8 deletions backend/app/Domain/Usecases/AuthRegister/AuthRegisterUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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(),
]));
}
}
7 changes: 1 addition & 6 deletions backend/app/Http/Controllers/AuthLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions backend/app/Http/Controllers/AuthRegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 57c140c

Please sign in to comment.