-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (25 loc) · 1.01 KB
/
Copy pathmiddleware.ts
File metadata and controls
31 lines (25 loc) · 1.01 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function middleware(req: NextRequest) {
// Keep middleware minimal for Vercel Edge size limit and avoid
// false negatives from token decoding differences in Auth.js v5.
const hasSessionCookie =
req.cookies.has("__Secure-authjs.session-token") ||
req.cookies.has("authjs.session-token");
const isLoggedIn = hasSessionCookie;
const { pathname, search } = req.nextUrl;
const isProtected =
pathname.startsWith("/dashboard") || pathname.startsWith("/tasks");
if (isProtected && !isLoggedIn) {
const url = new URL("/login", req.nextUrl.origin);
url.searchParams.set("callbackUrl", `${pathname}${search}`);
return NextResponse.redirect(url);
}
if (pathname.startsWith("/login") && isLoggedIn) {
return NextResponse.redirect(new URL("/tasks", req.nextUrl.origin));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/tasks/:path*", "/login"],
};