-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmiddleware.ts
More file actions
46 lines (37 loc) · 1.15 KB
/
middleware.ts
File metadata and controls
46 lines (37 loc) · 1.15 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
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({ req, res });
const {
data: { session },
} = await supabase.auth.getSession();
const protectedRoutes = [
'/api/profile/update',
'/api/resume',
'/api/members',
'/api/experiences',
'/api/achievements',
'/api/links',
'/api/member-skills',
];
const pathname = req.nextUrl.pathname;
const isProtected = protectedRoutes.some((route) =>
pathname.startsWith(route)
);
if (isProtected && !session) {
const redirectUrl = new URL('/auth/email-link-sign-in', req.url);
redirectUrl.searchParams.set('redirect', pathname);
return NextResponse.redirect(redirectUrl);
}
if (pathname.startsWith('/auth') && session) {
return NextResponse.redirect(new URL('/', req.url));
}
return res;
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|images|fonts|public|api/public).*)',
],
};