Phase 3. 인증 및 공통 레이아웃 작업
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
|
||||
const PUBLIC_PATHS = ["/login"];
|
||||
|
||||
export async function proxy(request: NextRequest) {
|
||||
const { pathname, search } = request.nextUrl;
|
||||
const isPublicPath = PUBLIC_PATHS.includes(pathname);
|
||||
const token = await getToken({
|
||||
req: request,
|
||||
secret: process.env.AUTH_SECRET ?? process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
|
||||
if (!token && !isPublicPath) {
|
||||
const loginUrl = new URL("/login", request.url);
|
||||
loginUrl.searchParams.set("callbackUrl", `${pathname}${search}`);
|
||||
|
||||
return NextResponse.redirect(loginUrl);
|
||||
}
|
||||
|
||||
if (token && isPublicPath) {
|
||||
return NextResponse.redirect(new URL("/", request.url));
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico|.*\\..*).*)"],
|
||||
};
|
||||
Reference in New Issue
Block a user