-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
45 lines (37 loc) · 1.06 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
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getSession } from '@auth0/nextjs-auth0/edge';
const publicRoutes = [
'/',
'/api/auth/login',
'/api/auth/logout',
'/api/auth/callback',
'/api/auth/signup',
'/api/auth/error'
];
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (publicRoutes.includes(pathname)) {
return NextResponse.next();
}
if (pathname.startsWith('/api/')) {
return NextResponse.next();
}
try {
const response = NextResponse.next();
const session = await getSession(request, response);
if (!session?.user) {
return NextResponse.redirect(new URL('/api/auth/login', request.url));
}
return response;
} catch (error) {
console.error('Session verification error:', error);
return NextResponse.redirect(new URL('/api/auth/login', request.url));
}
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)'
]
};