|
@@ -9,10 +9,8 @@
|
|
|
use App\Http\Resources\AuthResource;
|
|
use App\Http\Resources\AuthResource;
|
|
|
use App\Services\AuthService;
|
|
use App\Services\AuthService;
|
|
|
use App\DTO\RefreshTokenDTO;
|
|
use App\DTO\RefreshTokenDTO;
|
|
|
-use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class AuthController extends Controller
|
|
class AuthController extends Controller
|
|
|
-
|
|
|
|
|
{
|
|
{
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
protected AuthService $authService,
|
|
protected AuthService $authService,
|
|
@@ -20,27 +18,57 @@ public function __construct(
|
|
|
|
|
|
|
|
public function login(AuthRequest $request): JsonResponse
|
|
public function login(AuthRequest $request): JsonResponse
|
|
|
{
|
|
{
|
|
|
- $tokens = $this->authService->login(credentials: AuthDTO::fromRequest(request: $request));
|
|
|
|
|
|
|
+ $result = $this->authService->login(credentials: AuthDTO::fromRequest(request: $request));
|
|
|
|
|
|
|
|
- if (!$tokens) {
|
|
|
|
|
- return $this->errorResponse(message: __(key: 'auth.failed'), code: 401);
|
|
|
|
|
|
|
+ if (!$result) {
|
|
|
|
|
+ return $this->errorResponse(message: __('auth.failed'), code: 401);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return $this->successResponse(payload: new AuthResource(resource: $tokens), message: __(key: 'auth.logged_in'));
|
|
|
|
|
|
|
+ return $this->successResponse(payload: new AuthResource($result['payload']), message: __('auth.logged_in'))
|
|
|
|
|
+ ->withCookie(
|
|
|
|
|
+ cookie(
|
|
|
|
|
+ 'refresh_token',
|
|
|
|
|
+ $result['refreshToken'],
|
|
|
|
|
+ config('sanctum.rt_expiration') * 60,
|
|
|
|
|
+ '/',
|
|
|
|
|
+ config('session.domain'),
|
|
|
|
|
+ config('session.secure'),
|
|
|
|
|
+ true,
|
|
|
|
|
+ false,
|
|
|
|
|
+ 'Lax'
|
|
|
|
|
+ )
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function logout(): JsonResponse
|
|
public function logout(): JsonResponse
|
|
|
{
|
|
{
|
|
|
$this->authService->logout();
|
|
$this->authService->logout();
|
|
|
- return $this->successResponse(message: __(key: 'auth.logout'));
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return $this->successResponse(message: __('auth.logout'))
|
|
|
|
|
+ ->withoutCookie('refresh_token');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function refresh(RefreshTokenRequest $request): JsonResponse
|
|
public function refresh(RefreshTokenRequest $request): JsonResponse
|
|
|
{
|
|
{
|
|
|
- $tokens = $this->authService->refresh(refreshToken: RefreshTokenDTO::fromRequest(request: $request));
|
|
|
|
|
- if (is_null(value: $tokens)) {
|
|
|
|
|
- return $this->errorResponse(message: __(key: 'auth.unauthorized'), code: 403);
|
|
|
|
|
|
|
+ $result = $this->authService->refresh(RefreshTokenDTO::fromRequest(request: $request));
|
|
|
|
|
+ if (is_null($result)) {
|
|
|
|
|
+ return $this->errorResponse(message: __('auth.unauthorized'), code: 403)
|
|
|
|
|
+ ->withoutCookie('refresh_token');
|
|
|
}
|
|
}
|
|
|
- return $this->successResponse(payload: new AuthResource(resource: $tokens));
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return $this->successResponse(payload: new AuthResource($result['payload']))
|
|
|
|
|
+ ->withCookie(
|
|
|
|
|
+ cookie(
|
|
|
|
|
+ 'refresh_token',
|
|
|
|
|
+ $result['refreshToken'],
|
|
|
|
|
+ config('sanctum.rt_expiration') * 60,
|
|
|
|
|
+ '/',
|
|
|
|
|
+ config('session.domain'),
|
|
|
|
|
+ config('session.secure'),
|
|
|
|
|
+ true,
|
|
|
|
|
+ true,
|
|
|
|
|
+ 'Lax'
|
|
|
|
|
+ )
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|