-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
24 lines (19 loc) · 778 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
24 lines (19 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const token = request.cookies.get("next-auth.session-token");
const isAuthPage = request.nextUrl.pathname.startsWith("/auth");
if (!token && !isAuthPage) {
// Redirect to login if not authenticated
return NextResponse.redirect(new URL("/auth/signin", request.url));
}
if (token && isAuthPage) {
// Redirect to dashboard if authenticated and trying to access auth page
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();
}
// Apply middleware to specific routes
export const config = {
matcher: ["/dashboard/:path*", "/profile/:path*", "/auth/:path*"],
};