32 lines
867 B
TypeScript
32 lines
867 B
TypeScript
import { type NextRequest, NextResponse } from "next/server";
|
|||
|
|
import { getToken } from "next-auth/jwt";
|
||
|
|
|
||
|
|
export default async function proxy(req: NextRequest) {
|
||
|
|
const { pathname } = req.nextUrl;
|
||
|
|
|
||
|
|
const token = await getToken({
|
||
|
|
req,
|
||
|
|
secret: process.env.AUTH_SECRET,
|
||
|
|
secureCookie: process.env.NODE_ENV === "production",
|
||
|
|
});
|
||
|
|
|
||
|
|
const isLoggedIn = !!token;
|
||
|
|
const isDashboardRoute = pathname.startsWith("/dashboard");
|
||
|
|
const isAuthRoute =
|
||
|
|
pathname.startsWith("/login") || pathname.startsWith("/register");
|
||
|
|
|
||
|
|
if (isDashboardRoute && !isLoggedIn) {
|
||
|
|
return NextResponse.redirect(new URL("/login", req.url));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isAuthRoute && isLoggedIn) {
|
||
|
|
return NextResponse.redirect(new URL("/dashboard", req.url));
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
export const config = {
|
||
|
|
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
|
||
|
|
};
|