-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
190 lines (161 loc) · 5.6 KB
/
Copy pathmiddleware.ts
File metadata and controls
190 lines (161 loc) · 5.6 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Auth Middleware
*
* Server-side authentication checks for protected routes.
* Runs before page renders to ensure user is authenticated.
*/
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { createServerClient } from '@supabase/ssr';
// =============================================================================
// ROUTE CONFIGURATION
// =============================================================================
// Routes that require authentication
const PROTECTED_ROUTES = [
'/dashboard',
'/assessment',
'/quest', // Legacy route (deprecated)
'/reset', // Utility route for deleting user data (requires auth)
'/coach',
'/admin',
'/account',
];
// Routes that should redirect to dashboard if already authenticated
const AUTH_ROUTES = [
'/auth/login',
'/auth/signup',
];
// Public routes (no auth check needed)
const PUBLIC_ROUTES = [
'/',
'/auth/reset-password',
'/auth/callback',
'/api',
'/logout', // Utility route for signing out (no auth needed)
];
// =============================================================================
// MIDDLEWARE
// =============================================================================
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip middleware for static files and API routes
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/static') ||
pathname.includes('.') ||
pathname.startsWith('/api/')
) {
return NextResponse.next();
}
// Create Supabase client for middleware
let response = NextResponse.next({
request: {
headers: request.headers,
},
});
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => {
request.cookies.set(name, value);
response.cookies.set(name, value, options);
});
},
},
}
);
// Get session
const { data: { session } } = await supabase.auth.getSession();
// Check if route is protected
const isProtectedRoute = PROTECTED_ROUTES.some(route => pathname.startsWith(route));
const isAuthRoute = AUTH_ROUTES.some(route => pathname.startsWith(route));
// =========================================================================
// REDIRECT LOGIC
// =========================================================================
// Protected route + no session = redirect to login
if (isProtectedRoute && !session) {
const loginUrl = new URL('/auth/login', request.url);
loginUrl.searchParams.set('returnUrl', pathname);
// Determine role from pathname for better UX
if (pathname.startsWith('/coach')) {
loginUrl.searchParams.set('role', 'coach');
} else if (pathname.startsWith('/admin')) {
loginUrl.searchParams.set('role', 'admin');
} else {
loginUrl.searchParams.set('role', 'student');
}
return NextResponse.redirect(loginUrl);
}
// Auth route + session = redirect to dashboard
if (isAuthRoute && session) {
// Get user profile to determine dashboard
const { data: profile } = await supabase
.from('profiles')
.select('role')
.eq('id', session.user.id)
.single();
const dashboardUrl = getDashboardUrl(profile?.role, request.url);
return NextResponse.redirect(dashboardUrl);
}
// =========================================================================
// ROLE-BASED ACCESS CONTROL
// =========================================================================
if (session) {
// Get user role from profile
const { data: profile } = await supabase
.from('profiles')
.select('role')
.eq('id', session.user.id)
.single();
const userRole = profile?.role;
// Coach routes require coach or admin role
if (pathname.startsWith('/coach') && !['coach', 'admin'].includes(userRole)) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
// Admin routes require admin role
if (pathname.startsWith('/admin') && userRole !== 'admin') {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
// Student routes allow only students
if ((pathname.startsWith('/dashboard') || pathname.startsWith('/assessment') || pathname.startsWith('/quest')) &&
userRole && !['student', 'admin'].includes(userRole)) {
return NextResponse.redirect(new URL('/coach', request.url));
}
}
return response;
}
// =============================================================================
// CONFIG
// =============================================================================
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder files
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
};
// =============================================================================
// HELPERS
// =============================================================================
function getDashboardUrl(role: string | undefined, baseUrl: string): URL {
switch (role) {
case 'coach':
return new URL('/coach', baseUrl);
case 'admin':
return new URL('/admin', baseUrl);
case 'student':
default:
return new URL('/dashboard', baseUrl);
}
}