-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
58 lines (48 loc) · 1.76 KB
/
middleware.js
File metadata and controls
58 lines (48 loc) · 1.76 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
47
48
49
50
51
52
53
54
55
56
57
58
import { getToken } from 'next-auth/jwt';
import { NextResponse } from 'next/server';
export async function middleware(req) {
const token = await getToken({ req });
const { pathname } = req.nextUrl;
// Public paths that don't require authentication
const publicPaths = ['/login', '/register', '/menu', '/about', '/contact', '/', '/api/auth'];
// Check if the current path is public
const isPublicPath = publicPaths.some(path => pathname.startsWith(path));
// Protected routes that require authentication
const protectedPaths = ['/cart', '/profile', '/orders'];
const isProtectedPath = protectedPaths.some(path => pathname.startsWith(path));
// If trying to access protected route without auth, redirect to login
if (isProtectedPath && !token) {
return NextResponse.redirect(new URL('/login', req.url));
}
// If trying to access auth pages while logged in, redirect to home
if ((pathname === '/login' || pathname === '/register') && token) {
return NextResponse.redirect(new URL('/', req.url));
}
// Allow access to public paths
if (isPublicPath) {
return NextResponse.next();
}
// Protect API routes except auth routes
if (pathname.startsWith('/api') && !pathname.startsWith('/api/auth')) {
if (!token) {
return NextResponse.json(
{ error: 'Authentication required' },
{ status: 401 }
);
}
}
return NextResponse.next();
}
// Update the matcher configuration
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder
*/
'/((?!_next/static|_next/image|favicon.ico|public/|assets/).*)',
],
};