first commit
This commit is contained in:
132
README.md
Normal file
132
README.md
Normal file
@@ -0,0 +1,132 @@
|
||||
# Sistema Auth Central (Nuxt + Prisma)
|
||||
|
||||
MVP de autenticação central para serviços distribuídos.
|
||||
|
||||
## O que este projeto entrega
|
||||
|
||||
- Auth Service com `login` e `refresh`
|
||||
- JWT assinado em `RS256` com contrato fixo
|
||||
- Middleware de validação JWT local
|
||||
- Serviço consumidor de referência (`/profile/me`)
|
||||
- Fluxo A -> B (`/dashboard` chama `/profile/me` com o mesmo Bearer token)
|
||||
|
||||
## Stack
|
||||
|
||||
- Nuxt 4 (Nitro server routes)
|
||||
- Prisma + Postgres
|
||||
- JOSE (JWT)
|
||||
|
||||
## Setup
|
||||
|
||||
1. Instale dependências:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
2. Gere chaves RSA para JWT:
|
||||
|
||||
```bash
|
||||
npm run jwt:keys
|
||||
```
|
||||
|
||||
Distribua o valor de `JWT_PUBLIC_KEY_PEM` para todos os serviços consumidores.
|
||||
|
||||
3. Crie `.env` a partir de `.env.example` e preencha os valores.
|
||||
Se usar Postgres remoto com pool/proxy (ex.: Railway), use `DIRECT_DATABASE_URL` com conexão direta para migrations.
|
||||
|
||||
4. Execute migrações do Prisma:
|
||||
|
||||
```bash
|
||||
npm run prisma:migrate:dev -- --name init
|
||||
```
|
||||
|
||||
5. Rode seed para usuários de teste:
|
||||
|
||||
```bash
|
||||
npm run prisma:seed
|
||||
```
|
||||
|
||||
6. Inicie a aplicação:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
## Usuários de seed
|
||||
|
||||
- `student@example.com` / `student123`
|
||||
- `admin@example.com` / `admin123`
|
||||
- `limited@example.com` / `limited123`
|
||||
|
||||
## Estrutura da tabela `User`
|
||||
|
||||
A tabela `User` possui apenas:
|
||||
|
||||
- `id`
|
||||
- `email`
|
||||
- `passwordHash`
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
## Endpoints
|
||||
|
||||
- `POST /auth/login`
|
||||
- `POST /auth/refresh`
|
||||
- `GET /profile/me` (protegida)
|
||||
- `GET /dashboard` (protegida, chama `/profile/me`)
|
||||
|
||||
## Guia para serviços consumidores
|
||||
|
||||
- `docs/GUIA_VALIDACAO_JWT_SERVICOS_CONSUMIDORES.md`
|
||||
|
||||
## Contrato do Access Token
|
||||
|
||||
Claims obrigatórias:
|
||||
|
||||
- `sub`
|
||||
- `iss`
|
||||
- `aud`
|
||||
- `iat`
|
||||
- `exp`
|
||||
|
||||
A identidade confiável do usuário é sempre o `sub`.
|
||||
|
||||
## Teste rápido (curl)
|
||||
|
||||
### 1) Login
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:3000/auth/login \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"email":"student@example.com","password":"student123"}'
|
||||
```
|
||||
|
||||
### 2) Rota protegida
|
||||
|
||||
```bash
|
||||
curl http://localhost:3000/profile/me \
|
||||
-H "Authorization: Bearer <access_token>"
|
||||
```
|
||||
|
||||
### 3) Chamada entre serviços (A -> B)
|
||||
|
||||
```bash
|
||||
curl http://localhost:3000/dashboard \
|
||||
-H "Authorization: Bearer <access_token>"
|
||||
```
|
||||
|
||||
### 4) Refresh
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:3000/auth/refresh \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"refresh_token":"<refresh_token>"}'
|
||||
```
|
||||
|
||||
## Scripts úteis
|
||||
|
||||
- `npm run prisma:generate`
|
||||
- `npm run prisma:migrate:dev`
|
||||
- `npm run prisma:seed`
|
||||
- `npm run jwt:keys`
|
||||
Reference in New Issue
Block a user