|
1 | | -import { NextResponse, NextRequest } from 'next/server'; |
2 | | -import { generateCsrfToken } from '@/lib/utils/csrf'; |
3 | | - |
4 | | -export async function GET(req: NextRequest) { |
5 | | - const token = req.cookies.get('auth_token')?.value; |
6 | | - const role = req.cookies.get('user_role')?.value; |
7 | | - |
8 | | - if (!token) { |
9 | | - return NextResponse.json({ error: 'Session expired' }, { status: 401 }); |
10 | | - } |
11 | | - |
12 | | - // In production, validate the token and fetch real user data. |
13 | | - // For mock/preview mode, return a session based on the cookie values. |
14 | | - return NextResponse.json({ |
15 | | - user: { |
16 | | - id: role === 'admin' ? 'admin-1' : 'GCCHHKNI7GRA5QWC7RCTT3OHO7SKAUMKQA6IBWEQEO2SXI3GF376UHDD', |
17 | | - email: role === 'admin' ? 'admin@bettapay.com' : 'merchant@bettapay.com', |
18 | | - name: role === 'admin' ? 'System Admin' : 'Merchant User', |
19 | | - role: role || 'merchant', |
20 | | - }, |
21 | | - token, |
22 | | - }); |
23 | | -} |
24 | | - |
25 | | -export async function POST(req: Request) { |
26 | | - try { |
27 | | - const body = await req.json(); |
28 | | - const token = body.token; |
29 | | - const role = body.role || ''; |
30 | | - |
31 | | - const res = NextResponse.json({ ok: true }); |
32 | | - |
33 | | - // Determine environment for Secure flag |
34 | | - const isProduction = process.env.NODE_ENV === 'production'; |
35 | | - const secureFlag = isProduction ? '; Secure' : ''; |
36 | | - |
37 | | - // Set HttpOnly cookie for auth token |
38 | | - res.headers.set('Set-Cookie', `auth_token=${token}; HttpOnly; Path=/; SameSite=Lax${secureFlag}`); |
39 | | - // Also set a non-HttpOnly role cookie so middleware/server-side can read role where needed |
40 | | - res.headers.append('Set-Cookie', `user_role=${role}; Path=/; SameSite=Lax${secureFlag}`); |
41 | | - // Set CSRF token cookie (non-HttpOnly so the client JS can read it for double-submit) |
42 | | - const csrfToken = generateCsrfToken(); |
43 | | - res.headers.append('Set-Cookie', `csrf_token=${csrfToken}; Path=/; SameSite=Strict; Max-Age=86400${secureFlag}`); |
44 | | - |
45 | | - return res; |
46 | | - } catch (error) { |
47 | | - console.error('Failed to set session:', error); |
48 | | - return NextResponse.json({ ok: false, error: 'Failed to set session' }, { status: 500 }); |
49 | | - } |
50 | | -} |
51 | | - |
52 | | -export async function DELETE() { |
53 | | - const res = NextResponse.json({ ok: true }); |
54 | | - const isProduction = process.env.NODE_ENV === 'production'; |
55 | | - const secureFlag = isProduction ? '; Secure' : ''; |
56 | | - // Clear cookies |
57 | | - res.headers.set('Set-Cookie', `auth_token=; HttpOnly; Path=/; Max-Age=0; SameSite=Lax${secureFlag}`); |
58 | | - res.headers.append('Set-Cookie', `user_role=; Path=/; Max-Age=0; SameSite=Lax${secureFlag}`); |
59 | | - res.headers.append('Set-Cookie', `csrf_token=; Path=/; Max-Age=0; SameSite=Strict${secureFlag}`); |
60 | | - return res; |
61 | | -} |
| 1 | +import { NextResponse, NextRequest } from 'next/server'; |
| 2 | +import { generateCsrfToken, buildCsrfCookieHeader } from '@/lib/utils/csrf'; |
| 3 | + |
| 4 | +export async function GET(req: NextRequest) { |
| 5 | + const token = req.cookies.get('auth_token')?.value; |
| 6 | + const role = req.cookies.get('user_role')?.value; |
| 7 | + |
| 8 | + if (!token) { |
| 9 | + return NextResponse.json({ error: 'Session expired' }, { status: 401 }); |
| 10 | + } |
| 11 | + |
| 12 | + // In production, validate the token and fetch real user data. |
| 13 | + // For mock/preview mode, return a session based on the cookie values. |
| 14 | + return NextResponse.json({ |
| 15 | + user: { |
| 16 | + id: role === 'admin' ? 'admin-1' : 'GCCHHKNI7GRA5QWC7RCTT3OHO7SKAUMKQA6IBWEQEO2SXI3GF376UHDD', |
| 17 | + email: role === 'admin' ? 'admin@bettapay.com' : 'merchant@bettapay.com', |
| 18 | + name: role === 'admin' ? 'System Admin' : 'Merchant User', |
| 19 | + role: role || 'merchant', |
| 20 | + }, |
| 21 | + token, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +export async function POST(req: Request) { |
| 26 | + try { |
| 27 | + const body = await req.json(); |
| 28 | + const token = body.token; |
| 29 | + const role = body.role || ''; |
| 30 | + |
| 31 | + const isProduction = process.env.NODE_ENV === 'production'; |
| 32 | + const secureFlag = isProduction ? '; Secure' : ''; |
| 33 | + |
| 34 | + // Rotate the CSRF token on every login — this is the primary token rotation |
| 35 | + // point. A fresh token is tied to the new authenticated session. |
| 36 | + const csrfToken = generateCsrfToken(); |
| 37 | + |
| 38 | + const res = NextResponse.json({ ok: true }); |
| 39 | + |
| 40 | + // auth_token: HttpOnly so JS cannot read it (XSS protection) |
| 41 | + res.headers.set( |
| 42 | + 'Set-Cookie', |
| 43 | + `auth_token=${token}; HttpOnly; Path=/; SameSite=Lax; Max-Age=86400${secureFlag}` |
| 44 | + ); |
| 45 | + // user_role: non-HttpOnly so middleware / server-side can read it |
| 46 | + res.headers.append( |
| 47 | + 'Set-Cookie', |
| 48 | + `user_role=${role}; Path=/; SameSite=Lax; Max-Age=86400${secureFlag}` |
| 49 | + ); |
| 50 | + // csrf_token: non-HttpOnly (JS must read it), SameSite=Strict |
| 51 | + res.headers.append('Set-Cookie', buildCsrfCookieHeader(csrfToken)); |
| 52 | + |
| 53 | + return res; |
| 54 | + } catch (error) { |
| 55 | + console.error('Failed to set session:', error); |
| 56 | + return NextResponse.json({ ok: false, error: 'Failed to set session' }, { status: 500 }); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export async function DELETE() { |
| 61 | + const isProduction = process.env.NODE_ENV === 'production'; |
| 62 | + const secureFlag = isProduction ? '; Secure' : ''; |
| 63 | + |
| 64 | + const res = NextResponse.json({ ok: true }); |
| 65 | + // Expire all three cookies atomically on logout |
| 66 | + res.headers.set( |
| 67 | + 'Set-Cookie', |
| 68 | + `auth_token=; HttpOnly; Path=/; Max-Age=0; SameSite=Lax${secureFlag}` |
| 69 | + ); |
| 70 | + res.headers.append( |
| 71 | + 'Set-Cookie', |
| 72 | + `user_role=; Path=/; Max-Age=0; SameSite=Lax${secureFlag}` |
| 73 | + ); |
| 74 | + res.headers.append( |
| 75 | + 'Set-Cookie', |
| 76 | + `csrf_token=; Path=/; Max-Age=0; SameSite=Strict${secureFlag}` |
| 77 | + ); |
| 78 | + return res; |
| 79 | +} |
0 commit comments