-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
45 lines (37 loc) · 1.14 KB
/
middleware.ts
File metadata and controls
45 lines (37 loc) · 1.14 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
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
export default withAuth(
function middleware(req) {
const { pathname } = req.nextUrl;
// Protect only dashboard routes
if (pathname.startsWith("/dashboard") && !req.nextauth.token) {
return NextResponse.redirect(new URL("/login", req.url));
}
return NextResponse.next();
},
{
callbacks: {
authorized({ req, token }) {
const { pathname } = req.nextUrl;
// If accessing dashboard, require login
if (pathname.startsWith("/dashboard")) {
return !!token;
}
// All other routes are public
return true;
},
},
}
);
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/).*)",
],
};