| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace App\Http\Controllers;
- use App\DataTransferObjects\AuthDto;
- use App\Http\Requests\AuthRequest;
- use App\Http\Requests\RefreshTokenRequest;
- use Illuminate\Http\JsonResponse;
- use App\Http\Resources\AuthResource;
- use App\Services\AuthService;
- use App\DataTransferObjects\RefreshTokenDto;
- class AuthController extends Controller
- {
- public function __construct(
- protected AuthService $authService,
- ) {}
- public function login(AuthRequest $request): JsonResponse
- {
- $tokens = $this->authService->login(AuthDto::fromRequest($request));
- if (!$tokens) {
- return $this->errorResponse(message: __('auth.failed'), code: 401);
- }
- return $this->successResponse(payload: new AuthResource($tokens), message: __('auth.logged_in'));
- }
- public function logout(): JsonResponse
- {
- $this->authService->logout();
- return $this->successResponse(message: __('auth.logout'));
- }
- public function refresh(RefreshTokenRequest $request): JsonResponse
- {
- $tokens = $this->authService->refresh(RefreshTokenDto::fromRequest($request));
- if (is_null($tokens)) {
- return $this->errorResponse(message: __('auth.unauthorized'), code: 403);
- }
- return $this->successResponse(payload: new AuthResource($tokens));
- }
- }
|