-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.ts
51 lines (40 loc) · 1.59 KB
/
middleware.ts
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
import { type NextRequest, NextResponse } from 'next/server';
import { updateSession } from '@/utils/supabase/middleware';
export async function middleware(request: NextRequest) {
const { supabase, response } = await updateSession(request);
// Check if the user is authenticated
const {
data: { user },
} = await supabase.auth.getUser();
// Define protected routes that require authentication
const protectedRoutes = ['/quizz', '/profile', '/settings', '/account'];
// Define auth routes (login/signup pages)
const authRoutes = ['/login', '/signup', '/auth/confirm'];
// Get current pathname
const pathname = request.nextUrl.pathname;
// Check if the current route is protected
const isProtectedRoute = protectedRoutes.some((route) =>
pathname.startsWith(route)
);
// Check if current route is an auth route
const isAuthRoute = authRoutes.some((route) => pathname.startsWith(route));
if (isProtectedRoute && !user) {
// If trying to access protected route while not authenticated,
// redirect to login page with return URL
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = '/login';
redirectUrl.searchParams.set('redirectedFrom', pathname);
return NextResponse.redirect(redirectUrl);
}
if (isAuthRoute && user) {
// If trying to access auth routes while authenticated,
// redirect to quizz page
return NextResponse.redirect(new URL('/quizz', request.url));
}
return response;
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|api/.*|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
};