-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
56 lines (47 loc) · 2.02 KB
/
proxy.ts
File metadata and controls
56 lines (47 loc) · 2.02 KB
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
56
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl
// Check for NextAuth session cookie (Auth.js v5 uses these cookie names)
const hasSession =
request.cookies.has('authjs.session-token') ||
request.cookies.has('__Secure-authjs.session-token') ||
request.cookies.has('next-auth.session-token')
const isPersonalized = request.cookies.get('takecare-personalized')?.value === 'true'
// 1. Protect /dashboard — must be signed in
if (pathname.startsWith('/dashboard')) {
if (!hasSession) {
return NextResponse.redirect(new URL('/signin', request.url))
}
// If signed in but NOT personalized, send to onboarding
if (!isPersonalized) {
return NextResponse.redirect(new URL('/personalization-choice', request.url))
}
}
// 2. If user is signed in + personalized and visits home or auth pages, redirect to dashboard
const authPages = ['/', '/signin', '/signup', '/personalization-choice', '/personalization-onboarding']
if (authPages.includes(pathname) && hasSession && isPersonalized) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
// 3. If user is on auth pages (signin/signup) but already signed in (not personalized), let them through
// to /personalization-choice and /personalization-onboarding
if (['/signin', '/signup'].includes(pathname) && hasSession) {
return NextResponse.redirect(new URL('/personalization-choice', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - onboarding (allow access to onboarding pages)
* - images, public assets
*/
'/((?!api|_next/static|_next/image|favicon.ico|onboarding|images|public).*)',
],
}
export default proxy;