|
1 | 1 | import type { IncomingMessage, ServerResponse } from "node:http"; |
2 | 2 | import { json } from "@delego/utils"; |
3 | | -import { registerUser, loginUser } from "../src/auth/authService.js"; |
| 3 | +import { registerUser, loginUser, refreshAccessToken } from "../src/auth/authService.js"; |
| 4 | +import { validateSchema, RegisterSchema, LoginSchema } from "../src/validation.js"; |
| 5 | +import { readJsonBody, InvalidJsonError, BodyTooLargeError } from "../src/request.js"; |
4 | 6 |
|
5 | | -async function readJsonBody(req: IncomingMessage): Promise<any> { |
6 | | - return new Promise((resolve, reject) => { |
7 | | - let body = ""; |
8 | | - req.on("data", (chunk) => { |
9 | | - body += chunk; |
10 | | - }); |
11 | | - req.on("end", () => { |
12 | | - try { |
13 | | - resolve(body ? JSON.parse(body) : {}); |
14 | | - } catch (err) { |
15 | | - reject(new Error("Invalid JSON body")); |
| 7 | +function parseCookies(req: IncomingMessage): Record<string, string> { |
| 8 | + const list: Record<string, string> = {}; |
| 9 | + const cookieHeader = req.headers.cookie; |
| 10 | + if (cookieHeader) { |
| 11 | + cookieHeader.split(";").forEach((cookie) => { |
| 12 | + const parts = cookie.split("="); |
| 13 | + if (parts.length >= 2) { |
| 14 | + const key = parts.shift()?.trim() ?? ""; |
| 15 | + const value = decodeURIComponent(parts.join("=").trim()); |
| 16 | + if (key) { |
| 17 | + list[key] = value; |
| 18 | + } |
16 | 19 | } |
17 | 20 | }); |
18 | | - req.on("error", (err) => { |
19 | | - reject(err); |
20 | | - }); |
21 | | - }); |
| 21 | + } |
| 22 | + return list; |
| 23 | +} |
| 24 | + |
| 25 | +function setRefreshTokenCookie(res: ServerResponse, refreshToken: string): void { |
| 26 | + const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000); |
| 27 | + const cookie = [ |
| 28 | + `refresh_token=${refreshToken}`, |
| 29 | + `Expires=${expires.toUTCString()}`, |
| 30 | + "HttpOnly", |
| 31 | + "Secure", |
| 32 | + "SameSite=Strict", |
| 33 | + "Path=/", |
| 34 | + ].join("; "); |
| 35 | + res.setHeader("Set-Cookie", cookie); |
22 | 36 | } |
23 | 37 |
|
24 | 38 | export async function registerHandler(req: IncomingMessage, res: ServerResponse): Promise<void> { |
25 | 39 | try { |
26 | | - const { email, password, displayName } = await readJsonBody(req); |
27 | | - const result = await registerUser(email, password, displayName); |
28 | | - json(res, 201, { data: result, error: null }); |
29 | | - } catch (err: any) { |
30 | | - json(res, 400, { |
31 | | - data: null, |
32 | | - error: { code: "BAD_REQUEST", message: err.message }, |
| 40 | + const body = await readJsonBody(req); |
| 41 | + const validation = validateSchema(RegisterSchema, body); |
| 42 | + if (!validation.valid) { |
| 43 | + json(res, 400, { |
| 44 | + data: null, |
| 45 | + error: { code: "VALIDATION_ERROR", message: "Invalid request body", details: validation.errors }, |
| 46 | + }); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const result = await registerUser(body.email, body.password, body.displayName); |
| 51 | + setRefreshTokenCookie(res, result.refreshToken); |
| 52 | + json(res, 201, { |
| 53 | + data: { |
| 54 | + user: result.user, |
| 55 | + accessToken: result.accessToken, |
| 56 | + expiresIn: result.expiresIn, |
| 57 | + }, |
| 58 | + error: null, |
33 | 59 | }); |
| 60 | + } catch (err: any) { |
| 61 | + if (err instanceof InvalidJsonError || err instanceof BodyTooLargeError) { |
| 62 | + json(res, 400, { |
| 63 | + data: null, |
| 64 | + error: { code: "VALIDATION_ERROR", message: err.message }, |
| 65 | + }); |
| 66 | + } else { |
| 67 | + json(res, 400, { |
| 68 | + data: null, |
| 69 | + error: { code: "BAD_REQUEST", message: err.message }, |
| 70 | + }); |
| 71 | + } |
34 | 72 | } |
35 | 73 | } |
36 | 74 |
|
37 | 75 | export async function loginHandler(req: IncomingMessage, res: ServerResponse): Promise<void> { |
38 | 76 | try { |
39 | | - const { email, password } = await readJsonBody(req); |
40 | | - const result = await loginUser(email, password); |
41 | | - json(res, 200, { data: result, error: null }); |
| 77 | + const body = await readJsonBody(req); |
| 78 | + const validation = validateSchema(LoginSchema, body); |
| 79 | + if (!validation.valid) { |
| 80 | + json(res, 400, { |
| 81 | + data: null, |
| 82 | + error: { code: "VALIDATION_ERROR", message: "Invalid request body", details: validation.errors }, |
| 83 | + }); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const result = await loginUser(body.email, body.password); |
| 88 | + setRefreshTokenCookie(res, result.refreshToken); |
| 89 | + json(res, 200, { |
| 90 | + data: { |
| 91 | + user: result.user, |
| 92 | + accessToken: result.accessToken, |
| 93 | + expiresIn: result.expiresIn, |
| 94 | + }, |
| 95 | + error: null, |
| 96 | + }); |
| 97 | + } catch (err: any) { |
| 98 | + if (err instanceof InvalidJsonError || err instanceof BodyTooLargeError) { |
| 99 | + json(res, 400, { |
| 100 | + data: null, |
| 101 | + error: { code: "VALIDATION_ERROR", message: err.message }, |
| 102 | + }); |
| 103 | + } else { |
| 104 | + json(res, 401, { |
| 105 | + data: null, |
| 106 | + error: { code: "UNAUTHORIZED", message: err.message }, |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export async function refreshHandler(req: IncomingMessage, res: ServerResponse): Promise<void> { |
| 113 | + try { |
| 114 | + const cookies = parseCookies(req); |
| 115 | + const refreshToken = cookies.refresh_token; |
| 116 | + |
| 117 | + if (!refreshToken) { |
| 118 | + json(res, 401, { |
| 119 | + data: null, |
| 120 | + error: { code: "UNAUTHORIZED", message: "Refresh token missing" }, |
| 121 | + }); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + const result = await refreshAccessToken(refreshToken); |
| 126 | + setRefreshTokenCookie(res, result.refreshToken); |
| 127 | + json(res, 200, { |
| 128 | + data: { |
| 129 | + accessToken: result.accessToken, |
| 130 | + expiresIn: result.expiresIn, |
| 131 | + }, |
| 132 | + error: null, |
| 133 | + }); |
42 | 134 | } catch (err: any) { |
43 | 135 | json(res, 401, { |
44 | 136 | data: null, |
|
0 commit comments