Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
109 lines (93 loc) · 4 KB
/
Copy pathproxy.ts
File metadata and controls
109 lines (93 loc) · 4 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
import { createServerClient, type CookieOptions } from "@supabase/ssr"
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
import type { Database } from "@/types/supabase"
export async function proxy(request: NextRequest) {
let response = NextResponse.next({ request })
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || process.env.SUPABASE_ANON_KEY
if (!supabaseUrl || !supabaseKey) {
return response
}
// Get the pathname
const { pathname } = request.nextUrl
// Auth routes that don't require authentication
const authRoutes = ["/login", "/signup", "/forgot-password", "/reset-password"]
const isAuthRoute = authRoutes.some((route) => pathname.startsWith(route))
// Auth callback route
const isAuthCallback = pathname.startsWith("/auth/callback")
// Public routes that don't require authentication
const publicRoutes = ["/", "/about", "/contact", "/pricing", "/blog", "/try"]
const isPublicFile = /\.(png|jpg|jpeg|gif|svg|ico|txt|xml|html|webmanifest|json)$/i.test(pathname)
const isPublicRoute =
publicRoutes.some((route) => pathname === route) || pathname.startsWith("/api/") || isPublicFile
// Check if any Supabase auth cookies are present in the request
const allCookies = request.cookies.getAll()
const hasAuthCookies = allCookies.some(
(cookie) => cookie.name.startsWith("sb-") && cookie.name.includes("-auth-token")
)
// Fast path: if no auth cookies exist, avoid unnecessary network calls to Supabase
if (!hasAuthCookies) {
// Unauthenticated user accessing protected route -> redirect immediately to /login
if (!isAuthRoute && !isPublicRoute && !isAuthCallback) {
const redirectUrl = new URL("/login", request.url)
return NextResponse.redirect(redirectUrl)
}
// Public or auth routes -> allow immediately with zero network latency
return response
}
// If auth cookies are present, verify user session with a timeout & try/catch guard
let user = null
try {
const supabase = createServerClient<Database>(supabaseUrl, supabaseKey, {
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet: { name: string; value: string; options: CookieOptions }[]) {
cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value))
response = NextResponse.next({ request })
cookiesToSet.forEach(({ name, value, options }) => {
response.cookies.set(name, value, options)
})
},
},
})
// Guard with a 2.5-second timeout so middleware never exhausts Vercel's 10s budget
const authTimeout = new Promise<{ data: { user: null }; error: Error }>((_, reject) =>
setTimeout(() => reject(new Error("Supabase auth verification timed out")), 2500)
)
const result = (await Promise.race([supabase.auth.getUser(), authTimeout])) as {
data: { user: any }
error?: any
}
user = result?.data?.user ?? null
} catch (error) {
console.error("Middleware Supabase auth verification failed or timed out:", error)
user = null
}
// If user is not authenticated and trying to access a protected route
if (!user && !isAuthRoute && !isPublicRoute && !isAuthCallback) {
const redirectUrl = new URL("/login", request.url)
return NextResponse.redirect(redirectUrl)
}
// If user is authenticated and trying to access auth routes
if (user && isAuthRoute) {
const redirectUrl = new URL("/dashboard", request.url)
return NextResponse.redirect(redirectUrl)
}
return response
}
// Specify which paths this proxy should run on
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)
* - public (public files)
*/
"/((?!_next/static|_next/image|favicon.ico|public).*)",
],
}