TIL 115. 2024-06-13

이준구·2024년 7월 21일
0

TIL 순서

목록 보기
117/119
post-thumbnail
  • middleware에서 request 쿠키 값으로 페이지 접근 방지
import { updateSession } from "@/utils/supabase/middleware";
import { NextResponse, type NextRequest } from "next/server";

// 클라이언트의 요청이 서버에 도착하기 전 또는 응답이 클라이언트에게 전송되기 전에 미들웨어(중간 역할)가 실행됩니다.
export async function middleware(request: NextRequest) {
  await updateSession(request);

  const loginCookie = request.cookies.get("sb-ktfrmyssyzqmoljohixh-auth-token");
  const socialCookie = request.cookies.get("sb-ktfrmyssyzqmoljohixh-auth-token0");
  const url = request.nextUrl.pathname;

  //로그인된 상태에서 login 페이지 접근 제어
  if (url == "/login" && (loginCookie || socialCookie)) {
    const url = request.nextUrl.clone();
    url.pathname = "/main";
    return NextResponse.redirect(url);
  }

  //로그인된 상태에서 register 페이지 접근 제어
  if (url == "/register" && (loginCookie || socialCookie)) {
    const url = request.nextUrl.clone();
    url.pathname = "/main";
    return NextResponse.redirect(url);
  }
}

// 아래의 경로를 제외한 모든 경로에서 미들웨어를 실행
// 이미지 파일 (svg, png, jpg, jpeg, gif, webp)
export const config = {
  matcher: ["/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)"]
};
profile
개발 중~~~ 내 자신도 발전 중😂🤣

0개의 댓글

관련 채용 정보