-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (33 loc) · 1.2 KB
/
middleware.ts
File metadata and controls
41 lines (33 loc) · 1.2 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { verifyToken } from '@/lib/auth/session';
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Routes publiques
if (pathname.startsWith('/sign-in') || pathname.startsWith('/signup')) {
return NextResponse.next();
}
// Protéger /admin/*
if (pathname.startsWith('/admin')) {
const sessionCookie = request.cookies.get('session');
if (!sessionCookie) {
return NextResponse.redirect(new URL('/sign-in', request.url));
}
try {
const session = await verifyToken(sessionCookie.value);
if (!session || new Date(session.expires) < new Date()) {
const response = NextResponse.redirect(new URL('/sign-in', request.url));
response.cookies.delete('session');
return response;
}
} catch {
const response = NextResponse.redirect(new URL('/sign-in', request.url));
response.cookies.delete('session');
return response;
}
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)'],
};