-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
107 lines (93 loc) · 3.2 KB
/
Copy pathmiddleware.ts
File metadata and controls
107 lines (93 loc) · 3.2 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
import { NextResponse, type NextRequest } from 'next/server'
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { SecurityHeaders } from '@/lib/auth-security'
export async function middleware(request: NextRequest) {
// 🚦 Skip auth for AssemblyAI callbacks
if (request.nextUrl.pathname.startsWith('/api/transcription/callback')) {
console.log('🪶 [MIDDLEWARE] Bypassing auth for webhook')
return NextResponse.next()
}
const response = NextResponse.next()
// 🧠 Attach Supabase with proper cookie management
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get: (name: string) => request.cookies.get(name)?.value,
set: (name: string, value: string, options: CookieOptions) => {
response.cookies.set({ name, value, ...options })
},
remove: (name: string, options: CookieOptions) => {
response.cookies.set({ name, value: '', ...options })
},
},
}
)
const { data, error } = await supabase.auth.getUser()
const user = data?.user ?? null
// 🧩 Define public routes
const publicRoutes = [
'/login',
'/signup',
'/forgot-password',
'/auth/callback',
'/',
'/features',
'/pricing',
'/about',
'/help',
'/contact',
'/privacy',
'/terms'
]
// Check if route is public
const path = request.nextUrl.pathname
const isPublic = publicRoutes.some(route => path.startsWith(route))
// 🔒 Auth Redirect Logic
if (!user && !isPublic && !path.startsWith('/api')) {
console.log(`🛡️ Redirecting unauthenticated request: ${path}`)
const url = request.nextUrl.clone()
url.pathname = '/login'
url.searchParams.set('redirect', path)
return NextResponse.redirect(url)
}
// 🚫 Prevent authenticated users from revisiting login/signup
if (
user &&
request.method === 'GET' &&
(path.startsWith('/login') || path.startsWith('/signup'))
) {
const role = (user.user_metadata?.role as 'student' | 'lecturer' | 'admin') || 'student'
const redirect =
role === 'lecturer'
? '/dashboard?role=lecturer'
: role === 'admin'
? '/dashboard?role=admin'
: '/dashboard?role=student'
return NextResponse.redirect(new URL(redirect, request.url))
}
// 👮 Role-based route guard
if (user && path.startsWith('/create-session')) {
const role = user.user_metadata?.role || 'student'
if (role !== 'lecturer' && role !== 'admin') {
console.log(`🛑 Unauthorized attempt by ${user.id.slice(0, 6)}`)
return NextResponse.redirect(new URL('/dashboard', request.url))
}
}
// 🧾 Attach user info headers for API routes
if (user && path.startsWith('/api')) {
response.headers.set('x-user-id', user.id)
response.headers.set('x-user-role', user.user_metadata?.role || 'student')
}
// 🛡️ Add final security headers
Object.entries(SecurityHeaders.getSecurityHeaders()).forEach(([k, v]) =>
response.headers.set(k, v)
)
return response
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
}