Skip to content

Commit

Permalink
Merge pull request #77 from School-of-Company/feature/private-url
Browse files Browse the repository at this point in the history
🔀 역할에 따른 url 처리 진행
  • Loading branch information
Ethen1264 authored Jan 16, 2025
2 parents 483d192 + b80a881 commit 7683573
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export function middleware(request: NextRequest) {
const accessToken = request.cookies.get('accessToken');
const MANAGE_RESTRICTED_PATHS = [
/^\/signIn$/,
/^\/signUp$/,
/^\/application\/.+\/(STANDARD|TRAINEE)$/,
];

const requestHeaders = new Headers(request.headers);
const USER_RESTRICTED_PATHS = [
/^\/admin$/,
/^\/create-exhibition$/,
/^\/expo-manage\/.+$/,
/^\/name-tag\/.+$/,
/^\/sms\/.+\/(STANDARD|TRAINEE)$/,
/^\/program(\/.*)?$/,
];

if (accessToken) {
requestHeaders.set('role', 'manage');
} else {
requestHeaders.set('role', 'user');
}
function handleApiRole(request: NextRequest): NextResponse {
const requestHeaders = new Headers(request.headers);
const role = request.cookies.get('accessToken') ? 'manage' : 'user';
requestHeaders.set('role', role);

return NextResponse.next({
request: {
Expand All @@ -19,6 +28,44 @@ export function middleware(request: NextRequest) {
});
}

function isPathMatch(pathname: string, patterns: RegExp[]): boolean {
return patterns.some((pattern) => pattern.test(pathname));
}

export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;

if (pathname === '/api/role') {
return handleApiRole(request);
}

const accessToken = request.cookies.get('accessToken');

if (!accessToken && isPathMatch(pathname, USER_RESTRICTED_PATHS)) {
return NextResponse.redirect(new URL('/', request.url));
}

if (accessToken && isPathMatch(pathname, MANAGE_RESTRICTED_PATHS)) {
return NextResponse.redirect(new URL('/', request.url));
}

return NextResponse.next();
}

export const config = {
matcher: ['/api/role'],
matcher: [
'/api/role',
'/signIn',
'/signUp',
'/admin',
'/create-exhibition',
'/expo-manage/:path*',
'/name-tag/:path*',
'/sms/:path*/STANDARD',
'/sms/:path*/TRAINEE',
'/program/:path*',
'/program/detail/:path*',
'/application/:path*/STANDARD',
'/application/:path*/TRAINEE',
],
};

0 comments on commit 7683573

Please sign in to comment.