-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
126 lines (109 loc) · 3.82 KB
/
proxy.ts
File metadata and controls
126 lines (109 loc) · 3.82 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { type NextRequest, NextResponse } from 'next/server'
// Public routes that don't require authentication
const PUBLIC_ROUTES = [
'/welcome',
'/sign-in',
'/verify-otp',
'/create-pin',
'/enter-pin',
'/forgot-pin',
'/onboarding',
'/register',
]
// Protected routes that require authentication
const PROTECTED_ROUTES = [
'/dashboard',
'/packages',
'/my-packages',
'/visits',
'/wallet',
'/profile',
'/family',
'/notifications',
]
// Routes that should redirect to welcome if already authenticated
const AUTH_ROUTES = ['/sign-in', '/verify-otp', '/create-pin', '/enter-pin']
// Routes that authenticated users can access (success page)
const AUTH_SUCCESS_ROUTES = ['/register/success']
/**
* Check if the given path matches any of the public routes
*/
function isPublicRoute(pathname: string): boolean {
return PUBLIC_ROUTES.some(route => pathname === route || pathname.startsWith(`${route}/`))
}
/**
* Check if the given path is a protected route
*/
function isProtectedRoute(pathname: string): boolean {
return PROTECTED_ROUTES.some(route => pathname === route || pathname.startsWith(`${route}/`))
}
/**
* Check if the given path is an auth route (sign in flow)
*/
function isAuthRoute(pathname: string): boolean {
return AUTH_ROUTES.some(route => pathname === route || pathname.startsWith(`${route}/`))
}
/**
* Proxy function to protect routes and handle authentication
* Runs on the server before any page is rendered
*/
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl
// Get auth session from cookies
const authCookie = request.cookies.get('auth-session')
const isAuthenticated = !!authCookie?.value
// Root path redirects to welcome (handled by app/page.tsx), allow it through
if (pathname === '/') {
return NextResponse.next()
}
// Protect authenticated routes
if (isProtectedRoute(pathname) && !isAuthenticated) {
// User trying to access protected route without auth
const url = new URL('/welcome', request.url)
return NextResponse.redirect(url)
}
// Allow authenticated users to access success page (must check BEFORE public route redirect)
if (AUTH_SUCCESS_ROUTES.some(route => pathname.startsWith(route))) {
return NextResponse.next()
}
// Redirect authenticated users away from auth pages (except welcome and success pages)
if (isPublicRoute(pathname) && isAuthenticated && pathname !== '/welcome' && !AUTH_SUCCESS_ROUTES.some(route => pathname.startsWith(route))) {
// Already logged in, redirect to dashboard
const url = new URL('/dashboard', request.url)
return NextResponse.redirect(url)
}
// Validate session data if authenticated
if (isAuthenticated && authCookie?.value) {
try {
const sessionData = JSON.parse(authCookie.value)
// Check if session has expired
if (sessionData.expires_at && sessionData.expires_at < Date.now()) {
// Session expired, clear cookie and redirect
const response = NextResponse.redirect(new URL('/welcome', request.url))
response.cookies.delete('auth-session')
return response
}
} catch (error) {
// Invalid session cookie, clear it
const response = isProtectedRoute(pathname)
? NextResponse.redirect(new URL('/welcome', request.url))
: NextResponse.next()
response.cookies.delete('auth-session')
return response
}
}
return NextResponse.next()
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - Static asset extensions
* Feel free to modify this pattern to include more paths.
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
}