-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
73 lines (62 loc) · 2.24 KB
/
middleware.ts
File metadata and controls
73 lines (62 loc) · 2.24 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { createServerClient } from '@supabase/ssr'
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Only guard /admin routes (except /admin/login)
if (pathname.startsWith('/admin') && !pathname.startsWith('/admin/login')) {
try {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
if (!supabaseUrl || !supabaseAnonKey) {
console.error('❌ Missing Supabase credentials in middleware')
return NextResponse.redirect(new URL('/admin/login', request.url))
}
// Create a response to modify cookies
let response = NextResponse.next({
request: {
headers: request.headers,
},
})
// Create Supabase client with cookie handling
const supabase = createServerClient(supabaseUrl, supabaseAnonKey, {
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) =>
request.cookies.set(name, value)
)
response = NextResponse.next({
request,
})
cookiesToSet.forEach(({ name, value, options }) =>
response.cookies.set(name, value, options)
)
},
},
})
// Check if user is authenticated
const { data: { user }, error } = await supabase.auth.getUser()
if (error || !user) {
// Not authenticated, redirect to login
const loginUrl = new URL('/admin/login', request.url)
loginUrl.searchParams.set('redirectTo', pathname)
return NextResponse.redirect(loginUrl)
}
// User is authenticated, allow request to proceed
// Admin role check happens in the page component for security-through-ambiguity
return response
} catch (error) {
console.error('❌ Middleware error:', error)
return NextResponse.redirect(new URL('/admin/login', request.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: [
'/admin/:path*',
],
}