forked from kenny-io/moqa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
50 lines (42 loc) · 1.29 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
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// Array of public paths that don't require authentication
const publicPaths = [
'/',
'/auth/sign-in',
'/auth/sign-up',
'/auth/callback',
'/docs', // Add docs to public paths
];
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({ req, res });
// Check if the path is public
const isPublicPath = publicPaths.some(path =>
req.nextUrl.pathname === path || req.nextUrl.pathname.startsWith('/docs/')
);
if (isPublicPath) {
return res;
}
const {
data: { session },
} = await supabase.auth.getSession();
// If no session and trying to access protected route, redirect to login
if (!session && !isPublicPath) {
return NextResponse.redirect(new URL('/auth/sign-in', req.url));
}
return res;
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
};