-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
33 lines (27 loc) · 879 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
33 lines (27 loc) · 879 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
25
26
27
28
29
30
31
32
33
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Allow internals, public assets and auth endpoints
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/api") ||
pathname.startsWith("/login") ||
pathname.startsWith("/public") ||
pathname === "/favicon.ico" ||
pathname.includes(".")
) {
return NextResponse.next();
}
const token = req.cookies.get("accessToken");
if (!token) {
const loginUrl = new URL("/login", req.nextUrl.origin);
loginUrl.searchParams.set("redirect", pathname);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
// apply to all routes — we short-circuit inside handler for allowed paths
matcher: "/:path*",
};