1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/**
* auth.routes.ts
*
* Rotas públicas e protegidas de autenticação:
* POST /api/v1/auth/register → cria conta
* POST /api/v1/auth/login → retorna JWT
* GET /api/v1/auth/me → dados do usuário logado (protegida)
*/
import type { FastifyInstance } from 'fastify';
import {
registerBodySchema,
loginBodySchema,
} from './auth.schema.js';
import {
registerHandler,
loginHandler,
meHandler,
} from './auth.controller.js';
async function authRoutes(fastify: FastifyInstance): Promise<void> {
// ── POST /auth/register ────────────────────────────────────────────────────
fastify.post('/register', {
schema: {
tags: ['Auth'],
summary: 'Registra um novo usuário',
body: registerBodySchema,
},
handler: registerHandler,
});
// ── POST /auth/login ───────────────────────────────────────────────────────
fastify.post('/login', {
schema: {
tags: ['Auth'],
summary: 'Autentica e retorna um JWT',
body: loginBodySchema,
},
handler: loginHandler,
});
// ── GET /auth/me ───────────────────────────────────────────────────────────
fastify.get('/me', {
schema: {
tags: ['Auth'],
summary: 'Retorna o usuário autenticado',
security: [{ bearerAuth: [] }],
},
preHandler: [fastify.authenticate],
handler: meHandler,
});
}
export default authRoutes;
|