All files / src proxy.ts

0% Statements 0/36
0% Branches 0/1
0% Functions 0/1
0% Lines 0/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                                                         
import { NextRequest, NextResponse } from 'next/server';

export const proxy = (request: NextRequest) => {
  const accessToken = request.cookies.get('accessToken');
  const refreshToken = request.cookies.get('refreshToken');

  const protectedPaths = ['/mypage', '/create-group', '/message', '/schedule', '/notification'];
  const isProtected = protectedPaths.some((path) => request.nextUrl.pathname.startsWith(path));

  const publicPaths = ['/login', '/signup'];
  const isPublic = publicPaths.some((path) => request.nextUrl.pathname.startsWith(path));

  // 인증된 사용자가 public 페이지 접근 시 홈으로
  if (isPublic && (accessToken || refreshToken)) {
    return NextResponse.redirect(new URL('/', request.url));
  }

  // 보호되지 않은 경로는 그냥 통과
  if (!isProtected) {
    return NextResponse.next();
  }

  // 둘 다 없으면 로그인 페이지로 redirect
  if (!accessToken && !refreshToken) {
    const loginUrl = new URL('/login', request.url);
    loginUrl.searchParams.set('error', 'unauthorized');
    loginUrl.searchParams.set('path', request.nextUrl.pathname);
    return NextResponse.redirect(loginUrl);
  }

  return NextResponse.next();
};

export const config = {
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};