first commit
This commit is contained in:
54
middleware.ts
Normal file
54
middleware.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
// src/middleware.ts
|
||||
import { auth } from "@/auth";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
|
||||
|
||||
|
||||
export default auth(async (req) => {
|
||||
// 세션 가져오기
|
||||
const session = req.auth;
|
||||
|
||||
const { pathname } = req.nextUrl;
|
||||
|
||||
|
||||
if (pathname.startsWith("/admin")) {
|
||||
const email = session?.user?.email;
|
||||
if (email !== "wsx204@naver.com") {
|
||||
return NextResponse.redirect(new URL("/", req.url));
|
||||
}
|
||||
}
|
||||
|
||||
if (pathname.startsWith("/api/admin")) {
|
||||
const email = session?.user?.email;
|
||||
if (email !== "wsx204@naver.com") {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 이미 로그인된 경우 루트 경로로 접근 시 대시보드로 리다이렉트
|
||||
if (session) {
|
||||
if (pathname === "/") {
|
||||
return NextResponse.redirect(new URL("/usr/1_dashboard", req.url));
|
||||
}
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 예외: 루트(/), 로그인(/login), api/auth 경로는 리다이렉트 안 함
|
||||
if (
|
||||
pathname === "/" ||
|
||||
pathname.startsWith("/login") ||
|
||||
pathname.startsWith("/api/auth")
|
||||
) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// 그 외 로그인 안 된 요청은 / 로 리다이렉트
|
||||
return NextResponse.redirect(new URL("/", req.url));
|
||||
});
|
||||
|
||||
// 모든 경로에 적용
|
||||
export const config = {
|
||||
matcher: ["/((?!_next|.*\\..*).*)"], // _next/static, 이미지, 파일 등 제외
|
||||
};
|
||||
Reference in New Issue
Block a user