-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.js
More file actions
29 lines (21 loc) · 763 Bytes
/
Copy pathmiddleware.js
File metadata and controls
29 lines (21 loc) · 763 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
import { NextResponse } from "next/server";
export function middleware(req) {
const token = req.cookies.get("id_token")?.value;
const { pathname, searchParams } = req.nextUrl;
const isAuthPage = pathname === "/login" || pathname === "/register";
if (!token && pathname.startsWith("/home")) {
const url = req.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("next", pathname + (searchParams.toString() ? `?${searchParams}` : ""));
return NextResponse.redirect(url);
}
if (token && isAuthPage) {
const url = req.nextUrl.clone();
url.pathname = "/home";
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: ["/home/:path*", "/login", "/register"],
};