Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/assets/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ export default function ProtectedRoute({
? requiredRole
: [requiredRole];

const hasRole = required.some((r) => userRoles.includes(r));
const normalize = (r: string) => r.replace(/^ROLE_/i, '').toUpperCase();

const normalizedUserRoles = userRoles.map((r) => normalize(r));
const normalizedRequired = required.map((r) => normalize(r));

const hasRole = normalizedRequired.some((req) =>
Comment on lines +33 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

역할을 확인할 때 Set을 사용하면 더 효율적입니다. userRolesSet으로 변환하면 각 requiredRole에 대한 확인이 평균 O(1) 시간 복잡도로 이루어집니다. 현재 구현은 O(M*N)의 복잡도를 가지지만, Set을 사용하면 O(M+N)으로 개선할 수 있습니다. 역할 목록이 작을 때는 큰 차이가 없겠지만, 더 나은 성능과 확장성을 위해 Set을 사용하는 것이 좋습니다.

    const normalizedUserRolesSet = new Set(userRoles.map((r) => normalize(r)));
    const hasRole = required.some((req) =>
      normalizedUserRolesSet.has(normalize(req))
    );

normalizedUserRoles.includes(req)
);

if (!hasRole) {
return <NotFound />;
}
Expand Down
Loading