File manager - Edit - /var/www/html/pcadigital/api/app/Http/Controllers/AuthController.php
Back
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; use App\Http\Services\AuthService; use Exception; use Illuminate\Support\Facades\Validator; /** * @OA\Tag( * name="Auth", * description="Gerenciamento de autenticação" * ) */ class AuthController extends Controller { private AuthService $authService; public function __construct() { $this->authService = new AuthService(); } /** * @OA\Post( * path="/api/login", * summary="Autenticação do usuário", * description="Realiza o login e retorna o token de acesso", * operationId="Login", * tags={"Auth"}, * @OA\RequestBody( * required=true, * @OA\MediaType( * mediaType="application/json", * @OA\Schema( * required={"username", "password"}, * @OA\Property(property="username", type="string", example="user@example.com"), * @OA\Property(property="password", type="string", example="password123") * ) * ) * ), * @OA\Response( * response=200, * description="Login realizado com sucesso" * ), * @OA\Response( * response=401, * description="Credenciais inválidas" * ) * ) */ public function login(Request $request): JsonResponse { $validator = Validator::make($request->all(), [ 'cpf' => 'required|string|max:255', 'password' => 'required|string' ]); if ($validator->fails()) { return response()->json( ['errors' => $validator->errors()], JsonResponse::HTTP_UNPROCESSABLE_ENTITY ); } try { $response = $this->authService->login($request); return response()->json($response->json(), $response->status()); } catch (Exception $e) { return response()->json( [$e->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } } /** * @OA\Post( * path="/api/logout", * summary="Logout do usuário", * description="Revoga o token de acesso do usuário", * operationId="Logout", * security={{"bearerAuth":{}}}, * tags={"Auth"}, * @OA\Response( * response=200, * description="Logout realizado com sucesso" * ), * @OA\Response( * response=401, * description="Não autorizado" * ) * ) */ public function logout(Request $request): JsonResponse { $response = $this->authService->logout($request); return response()->json(null, $response->status()); } /** * @OA\Get( * path="/api/session", * summary="Sessão do usuário", * description="Retorna os dados do usuário autenticado", * operationId="Session", * security={{"bearerAuth":{}}}, * tags={"Auth"}, * @OA\Response( * response=200, * description="Dados da sessão do usuário" * ) * ) */ public function session(Request $request): JsonResponse { return response()->json($request->user, JsonResponse::HTTP_OK); } /** * @OA\Post( * path="/api/send-email", * summary="Enviar e-mail de recuperação", * description="Envia um e-mail para redefinição de senha", * operationId="SendResetEmail", * tags={"Auth"}, * @OA\RequestBody( * required=true, * @OA\MediaType( * mediaType="application/json", * @OA\Schema( * required={"email"}, * @OA\Property(property="email", type="string", example="user@example.com") * ) * ) * ), * @OA\Response( * response=200, * description="E-mail enviado com sucesso" * ), * @OA\Response( * response=500, * description="Erro interno ao enviar e-mail" * ) * ) */ public function sendEmail(Request $request): JsonResponse { $validator = Validator::make($request->all(), [ 'cpf' => 'required|string|max:255', 'email' => 'required|string|email|max:255' ]); if ($validator->fails()) { return response()->json( ['errors' => $validator->errors()], JsonResponse::HTTP_UNPROCESSABLE_ENTITY ); } try { $response = $this->authService->sendEmail($request); return response()->json($response->json(), $response->status()); } catch (Exception $e) { return response()->json( [$e->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } } /** * @OA\Post( * path="/api/token-validator", * summary="Valida o token de redefinição de senha", * description="Verifica se o token informado é válido", * operationId="TokenValidator", * tags={"Auth"}, * @OA\RequestBody( * required=true, * @OA\MediaType( * mediaType="application/json", * @OA\Schema( * required={"token"}, * @OA\Property(property="token", type="string", example="abc123") * ) * ) * ), * @OA\Response( * response=200, * description="Token válido" * ), * @OA\Response( * response=400, * description="Token inválido ou expirado" * ) * ) */ public function tokenValidator(Request $request): JsonResponse { $validator = Validator::make($request->all(), [ 'code' => 'required|string|max:6' ]); if ($validator->fails()) { return response()->json( ['errors' => $validator->errors()], JsonResponse::HTTP_UNPROCESSABLE_ENTITY ); } try { $response = $this->authService->tokenValidator($request); return response()->json($response->json(), $response->status()); } catch (Exception $e) { return response()->json( [$e->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } } /** * @OA\Post( * path="/api/reset-password", * summary="Redefinição de senha", * description="Redefine a senha de um usuário com token válido", * operationId="ResetPassword", * tags={"Auth"}, * @OA\RequestBody( * required=true, * @OA\MediaType( * mediaType="application/json", * @OA\Schema( * required={"token", "password"}, * @OA\Property(property="token", type="string", example="abc123"), * @OA\Property(property="password", type="string", example="novaSenha123") * ) * ) * ), * @OA\Response( * response=200, * description="Senha redefinida com sucesso" * ), * @OA\Response( * response=400, * description="Token inválido ou dados incompletos" * ) * ) */ public function resetPassword(Request $request): JsonResponse { $validator = Validator::make($request->all(), [ 'new_password' => [ 'required', 'string', 'min:6', 'regex:/^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9!@#$%_-]+$/', 'not_regex:/[áàãâäéèêëíìîïóòõôöúùûüçÇ;,.]/u', 'confirmed' ], 'code' => 'required|string|max:6' ]); if ($validator->fails()) { return response()->json( ['errors' => $validator->errors()], JsonResponse::HTTP_UNPROCESSABLE_ENTITY ); } try { $response = $this->authService->resetPassword($request); return response()->json($response->json(), $response->status()); } catch (Exception $e) { return response()->json( [$e->getMessage()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings