|
1 | | -import type { FastifyPluginAsync } from "fastify"; |
2 | | -import { loginSchema, logoutSchema, meSchema, refreshSchema, registerSchema } from "./auth.schemas"; |
3 | | -import { loginUser, refreshUserTokens, registerUser, revokeRefreshToken } from "./auth.service"; |
4 | | -import type { LoginInput, RefreshInput, RegisterInput } from "./auth.types"; |
| 1 | +import type { |
| 2 | + FastifyPluginAsync |
| 3 | +} from "fastify"; |
| 4 | +import { |
| 5 | + getCurrentUserController |
| 6 | +} from "../users/user.controller"; |
| 7 | +import { |
| 8 | + loginController, |
| 9 | + logoutController, |
| 10 | + refreshController, |
| 11 | + registerController |
| 12 | +} from "./auth.controller"; |
| 13 | +import { |
| 14 | + loginSchema, |
| 15 | + logoutSchema, |
| 16 | + meSchema, |
| 17 | + refreshSchema, |
| 18 | + registerSchema |
| 19 | +} from "./auth.schemas"; |
| 20 | +import type { |
| 21 | + LoginInput, |
| 22 | + RefreshInput, |
| 23 | + RegisterInput |
| 24 | +} from "./auth.types"; |
5 | 25 |
|
6 | | -export const authRoutes: FastifyPluginAsync = async (app) => { |
7 | | - app.post<{ Body: RegisterInput }>( |
| 26 | +export const authRoutes: |
| 27 | +FastifyPluginAsync = async (app) => { |
| 28 | + app.post<{ |
| 29 | + Body: RegisterInput; |
| 30 | + }>( |
8 | 31 | "/register", |
9 | | - { schema: registerSchema }, |
10 | | - async (request, reply) => { |
11 | | - const result = await registerUser(app, request.body); |
12 | | - return reply.status(201).send(result); |
13 | | - } |
| 32 | + { |
| 33 | + schema: registerSchema |
| 34 | + }, |
| 35 | + registerController |
14 | 36 | ); |
15 | 37 |
|
16 | | - app.post<{ Body: LoginInput }>( |
| 38 | + app.post<{ |
| 39 | + Body: LoginInput; |
| 40 | + }>( |
17 | 41 | "/login", |
18 | | - { schema: loginSchema }, |
19 | | - async (request) => loginUser(app, request.body) |
| 42 | + { |
| 43 | + schema: loginSchema |
| 44 | + }, |
| 45 | + loginController |
20 | 46 | ); |
21 | 47 |
|
22 | | - app.post<{ Body: RefreshInput }>( |
| 48 | + app.post<{ |
| 49 | + Body: RefreshInput; |
| 50 | + }>( |
23 | 51 | "/refresh", |
24 | | - { schema: refreshSchema }, |
25 | | - async (request) => refreshUserTokens(app, request.body.refreshToken) |
| 52 | + { |
| 53 | + schema: refreshSchema |
| 54 | + }, |
| 55 | + refreshController |
26 | 56 | ); |
27 | 57 |
|
28 | | - app.post<{ Body: RefreshInput }>( |
| 58 | + app.post<{ |
| 59 | + Body: RefreshInput; |
| 60 | + }>( |
29 | 61 | "/logout", |
30 | | - { schema: logoutSchema }, |
31 | | - async (request, reply) => { |
32 | | - await revokeRefreshToken(app, request.body.refreshToken); |
33 | | - return reply.status(204).send(); |
34 | | - } |
| 62 | + { |
| 63 | + schema: logoutSchema |
| 64 | + }, |
| 65 | + logoutController |
35 | 66 | ); |
36 | 67 |
|
37 | 68 | app.get( |
38 | 69 | "/me", |
39 | | - { schema: meSchema, preHandler: [app.authenticate] }, |
40 | | - async (request) => app.prisma.user.findUniqueOrThrow({ |
41 | | - where: { id: request.user.sub }, |
42 | | - select: { |
43 | | - id: true, |
44 | | - email: true, |
45 | | - name: true, |
46 | | - role: true, |
47 | | - createdAt: true, |
48 | | - updatedAt: true |
49 | | - } |
50 | | - }) |
| 70 | + { |
| 71 | + schema: meSchema, |
| 72 | + preHandler: [ |
| 73 | + app.authenticate |
| 74 | + ] |
| 75 | + }, |
| 76 | + getCurrentUserController |
51 | 77 | ); |
52 | 78 | }; |
0 commit comments