init commit
All checks were successful
build-api-image / build (push) Successful in 51s

This commit is contained in:
Antonio Lopes dos Santos
2026-08-04 16:52:02 -03:00
commit afec302c3e
11594 changed files with 1636728 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Services\Mobile\ConectaCidadao\Autenticacao;
use App\Models\Cadastro\SujeitoAtencao\Cidadao;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;
class AutenticacaoService
{
public function login(array $dados): array
{
$cidadao = Cidadao::where('cpf', $dados['cpf'])
->whereHas('paciente', fn ($q) => $q->whereNotNull('senha'))
->first();
if (is_null($cidadao) || !Hash::check($dados['senha'], $cidadao->paciente->senha)) {
throw new \InvalidArgumentException('CPF e/ou senha incorreto(s).', Response::HTTP_UNAUTHORIZED);
}
$usuario = $cidadao->paciente;
$token = auth('mobile')->setTTL(15)->login($usuario);
$refreshToken = auth('mobile')
->claims(['type' => 'refresh'])
->setTTL(10080)
->tokenById($usuario->id);
return [
'expiration' => time() + 15 * 60,
'token' => $token,
'refresh_token' => $refreshToken,
'usuario' => [
'cpf' => $dados['cpf'],
'nome' => $cidadao->nome,
'paciente_id' => $cidadao->paciente->id,
],
];
}
public function refresh()
{
$payload = auth('mobile')->payload();
if ($payload->get('type') !== 'refresh') {
return response()->json(['error' => 'Token inválido'], 401);
}
$user = auth('mobile')->user();
auth('mobile')->invalidate();
$accessToken = auth('mobile')
->claims(['type' => 'access'])
->setTTL(15)
->tokenById($user->id);
$refreshToken = auth('mobile')
->claims(['type' => 'refresh'])
->setTTL(10080)
->tokenById($user->id);
return [
'token' => $accessToken,
'refresh_token' => $refreshToken,
'expiration' => time() + 15 * 60,
];
}
}