-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
24 lines (19 loc) · 710 Bytes
/
proxy.ts
File metadata and controls
24 lines (19 loc) · 710 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 { getToken } from "next-auth/jwt";
import { NextResponse, NextRequest } from "next/server";
export async function proxy(req: NextRequest) {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
const IsLoggedin = !!token;
const { pathname } = req.nextUrl;
// 🚫 Logged-out users trying to access protected pages
if (!IsLoggedin && pathname === "/") {
return NextResponse.redirect(new URL("/login", req.url));
}
// 🚫 Logged-in users trying to access auth pages
if (IsLoggedin && pathname === "/login") {
return NextResponse.redirect(new URL("/", req.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/", "/login"],
};