-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
59 lines (52 loc) · 1.63 KB
/
Copy pathmiddleware.ts
File metadata and controls
59 lines (52 loc) · 1.63 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
59
import { withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"
export default withAuth(
function middleware() {
return NextResponse.next();
},
{
callbacks: {
authorized: ({ token, req }) => {
const { pathname } = req.nextUrl;
// Allow webhook endpoint
if (pathname.startsWith("/api/webhook")) {
return true;
}
// Allow auth related routes
if (
pathname.startsWith("/api/auth") ||
pathname === "/login" ||
pathname === "/register"
) {
return true;
}
// Public routes
if (
pathname === "/" ||
pathname.startsWith("/api/products") ||
pathname.startsWith("/products")
) {
return true;
}
// Admin routes require admin role
if (pathname.startsWith("/admin")) {
return token?.role === "admin";
}
// All other routes require authntication
return !!token;
},
},
},
);
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|public/).*)",
],
};