Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”€ ์—ญํ•  ๊ฒ€์ฆ ๋กœ์ง ๊ฐœ์„  #72

Merged
merged 2 commits into from
Jan 10, 2025

Conversation

Ethen1264
Copy link
Contributor

@Ethen1264 Ethen1264 commented Jan 9, 2025

๐Ÿ’ก ๋ฐฐ๊ฒฝ ๋ฐ ๊ฐœ์š”

์—ญํ•  ๊ฒ€์ฆ ๋กœ์ง ๊ฐœ์„ 

๐Ÿ“ƒ ์ž‘์—…๋‚ด์šฉ

middlware๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ role์„ ๊ฒ€์ฆํ•จ
middleware์—์„œ ๊ฒ€์ฆํ•˜ role์„ route api์—์„œ ํ˜ธ์ถœํ•จ

๐ŸŽธ ๊ธฐํƒ€

#71
์ถ”ํ›„ ๋”์šฑ ์ข‹์€ ๋ฐฉ๋ฒ•์„ ์ฐพ์„ ํ•„์š”๊ฐ€ ์žˆ์–ด๋ณด์ž„

์ž‘์„ฑํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„์€ ์‚ญ์ œํ•˜์—ฌ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

Summary by CodeRabbit

  • ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ

    • ์‚ฌ์šฉ์ž ์—ญํ• (role) ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์ƒˆ๋กœ์šด API ์—”๋“œํฌ์ธํŠธ ์ถ”๊ฐ€
    • ์‚ฌ์šฉ์ž ์—ญํ• ์„ ๋™์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ์ƒˆ๋กœ์šด ํ›… ๋„์ž…
  • ๋ณ€๊ฒฝ ์‚ฌํ•ญ

    • ๊ธฐ์กด ํ† ํฐ ํ™•์ธ ๋ฐ ์ƒํƒœ ๊ด€๋ฆฌ ๋ฐฉ์‹ ๊ฐœ์„ 
    • ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์ปดํฌ๋„ŒํŠธ ์ œ๊ฑฐ
  • ๊ธฐํƒ€

    • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์—ญํ•  ๊ธฐ๋ฐ˜ ์ ‘๊ทผ ์ œ์–ด ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์—…๋ฐ์ดํŠธ

@Ethen1264 Ethen1264 added the โ™ป๏ธ Refactor ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง label Jan 9, 2025
@Ethen1264 Ethen1264 self-assigned this Jan 9, 2025
Copy link

coderabbitai bot commented Jan 9, 2025

Walkthrough

์ด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์—ญํ• (role) ๊ด€๋ฆฌ ๋ฐฉ์‹์„ ์žฌ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด์˜ Zustand ์Šคํ† ์–ด ๊ธฐ๋ฐ˜ ์ ‘๊ทผ ๋ฐฉ์‹์—์„œ ์ƒˆ๋กœ์šด useRole ํ›…๊ณผ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ†ตํ•œ ์—ญํ•  ๊ฒฐ์ • ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ ์ „ํ™˜ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์š” ๋ณ€๊ฒฝ์‚ฌํ•ญ์€ ์—ญํ•  ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” API ์—”๋“œํฌ์ธํŠธ ์ถ”๊ฐ€, ๋ฏธ๋“ค์›จ์–ด๋ฅผ ํ†ตํ•œ ์—ญํ•  ํ—ค๋” ์„ค์ •, ๊ทธ๋ฆฌ๊ณ  ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์ปดํฌ๋„ŒํŠธ ์ œ๊ฑฐ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

Changes

ํŒŒ์ผ ๋ณ€๊ฒฝ ์š”์•ฝ
src/api/role/route.ts ์ƒˆ๋กœ์šด ์—ญํ•  API ์—”๋“œํฌ์ธํŠธ ์ถ”๊ฐ€
src/middleware.ts ์š”์ฒญ ํ—ค๋”์— ์—ญํ•  ์„ค์ • ๋ฏธ๋“ค์›จ์–ด ๋„์ž…
src/shared/model/useRole.ts ์ƒˆ๋กœ์šด useRole ํ›… ์ƒ์„ฑ
src/entities/layout/Header/model/useNavigation.ts useStore ์—์„œ useRole๋กœ ์—ญํ•  ๊ฒ€์ƒ‰ ๋ฐฉ์‹ ๋ณ€๊ฒฝ
src/widgets/expo-detail/ui/ExpoActionPanel/index.tsx ์—ญํ•  ํ›… ๋ณ€๊ฒฝ ๋ฐ ClientInitializer ์ œ๊ฑฐ

Sequence Diagram

sequenceDiagram
    Client->>Middleware: Request
    Middleware->>Middleware: Check Access Token
    Middleware-->>Client: Set Role Header
    Client->>API: Request Role
    API-->>Client: Return Role
    Client->>Component: Use Role
Loading

Possibly related PRs

Suggested labels

๐Ÿ“ฌ Type: API

Suggested reviewers

  • answad
  • happytaeyoon

Poem

๐Ÿฐ ํ† ๋ผ์˜ ์ฝ”๋“œ ์ถค, ์—ญํ• ์˜ ๋ณ€์ฃผ
๋ฏธ๋“ค์›จ์–ด ์ถค์ถ”๊ณ  ํ›…์€ ๋…ธ๋ž˜ํ•ด
์˜ค๋ž˜๋œ ์Šคํ† ์–ด ์•ˆ๋…•, ์ƒˆ ๊ธธ์„ ๊ฐ€๋„ค
์—ญํ• ์€ ํ๋ฅด๊ณ , ์•ฑ์€ ๋น›๋‚˜๋„ค! ๐ŸŒŸ

Finishing Touches

  • ๐Ÿ“ Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

โค๏ธ Share
๐Ÿชง Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

๐Ÿ”ญ Outside diff range comments (2)
src/entities/layout/Header/model/useNavigation.ts (1)

Line range hint 8-20: ์—ญํ•  ์ƒํƒœ ์ฒ˜๋ฆฌ ๋ฐ ํƒ€์ž… ์•ˆ์ „์„ฑ ๊ฐœ์„  ํ•„์š”

ํ˜„์žฌ ๊ตฌํ˜„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐœ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค:

  1. ๋กœ๋”ฉ ๋ฐ ์—๋Ÿฌ ์ƒํƒœ ์ฒ˜๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค
  2. ์—ญํ•  ๊ฐ’์— ๋Œ€ํ•œ ํƒ€์ž… ์•ˆ์ „์„ฑ์ด ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค
  3. ํ•˜๋“œ์ฝ”๋”ฉ๋œ ์—ญํ•  ๋น„๊ต ๋กœ์ง์ด ์žˆ์Šต๋‹ˆ๋‹ค

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค:

+const VALID_ROLES = ['user', 'manage'] as const;
+type Role = typeof VALID_ROLES[number];
+
 export const useNavigation = () => {
-  const role = useRole();
+  const { role, isLoading, error } = useRole();
 
   const pathname = usePathname();
 
   const isActive = (path: string): boolean => pathname === path;
 
   const getColor = (path: string): string =>
     isActive(path) ? '#448FFF' : '#909090';
 
-  const navItems: NavItem[] =
-    role === 'user' ? userNavItems : role === 'manage' ? manageNavItems : [];
-  return { navItems, getColor };
+  const navItems: NavItem[] = isLoading
+    ? []
+    : error
+    ? userNavItems
+    : role === 'manage'
+    ? manageNavItems
+    : userNavItems;
+
+  return { navItems, getColor, isLoading, error };
 };
src/widgets/expo-detail/ui/ExpoActionPanel/index.tsx (1)

Line range hint 75-86: ์ผ๊ด€์„ฑ ์žˆ๋Š” ๋น„๊ต ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋‹ฌ ๋ Œ๋”๋ง ๋กœ์ง์—์„œ ์—ญํ•  ๋น„๊ต ์‹œ ์ผ๊ด€์„ฑ ์—†๋Š” ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค:

  • role === 'user'
  • role == 'manage' // ๋Š์Šจํ•œ ๋น„๊ต
- {role == 'manage' && (
+ {role === 'manage' && (
๐Ÿงน Nitpick comments (4)
src/middleware.ts (1)

22-24: ๋ฏธ๋“ค์›จ์–ด ๋งค์ฒ˜ ์„ค์ • ๊ฐœ์„  ํ•„์š”

ํ˜„์žฌ ๋งค์ฒ˜ ์„ค์ •์ด /api/role ๊ฒฝ๋กœ์—๋งŒ ์ œํ•œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ญํ•  ๊ธฐ๋ฐ˜ ์ ‘๊ทผ ์ œ์–ด๊ฐ€ ํ•„์š”ํ•œ ๋‹ค๋ฅธ API ์—”๋“œํฌ์ธํŠธ๋“ค๋„ ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

 export const config = {
-  matcher: ['/api/role'],
+  matcher: [
+    '/api/role',
+    '/api/manage/:path*',
+    '/api/protected/:path*'
+  ],
 };
src/widgets/expo-detail/ui/ExpoActionPanel/index.tsx (3)

5-5: ์—ญํ•  ๊ด€๋ฆฌ ๋กœ์ง์ด ๊ฐœ์„ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ๐Ÿ‘

useStore์—์„œ useRole ํ›…์œผ๋กœ์˜ ์ „ํ™˜์€ ์—ญํ•  ๊ด€๋ฆฌ๋ฅผ ๋” ๋ช…ํ™•ํ•˜๊ณ  ์ง‘์ค‘์ ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

๋‹ค๋งŒ, useRole ํ›…์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์— ๋Œ€ํ•œ JSDoc ๋ฌธ์„œํ™”๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค:

/**
 * @returns {'user' | 'manage' | undefined} ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ์—ญํ• 
 */

Also applies to: 15-15


Line range hint 21-61: ์ƒ์ˆ˜ ๋ถ„๋ฆฌ ๋ฐ ํƒ€์ž… ์•ˆ์ •์„ฑ ๊ฐœ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์—ญํ• ๋ณ„ ๋ฒ„ํŠผ ๋ Œ๋”๋ง ๋กœ์ง์—์„œ ๋‹ค์Œ ์‚ฌํ•ญ๋“ค์„ ๊ฐœ์„ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  1. ํ•˜๋“œ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์„ ์ƒ์ˆ˜๋กœ ๋ถ„๋ฆฌ
  2. ์—ญํ•  ํƒ€์ž…์„ ๋ช…์‹œ์ ์œผ๋กœ ์ •์˜
  3. ๋ผ์šฐํŠธ ๊ฒฝ๋กœ๋ฅผ ์ƒ์ˆ˜๋กœ ๋ถ„๋ฆฌ

์˜ˆ์‹œ ์ฝ”๋“œ:

const ROLES = {
  USER: 'user',
  MANAGE: 'manage'
} as const;

type Role = typeof ROLES[keyof typeof ROLES];

const ROUTES = {
  NAME_TAG: (id: number) => `/name-tag/${id}`,
  PROGRAM: (id: number) => `/program/${id}`,
  EXPO_MANAGE: (id: number) => `/expo-manage/${id}`,
} as const;

const MODAL_MESSAGES = {
  APPLICATION: 'ํ•ด๋‹น ๋ฐ•๋žŒํšŒ์— ์ง€์›ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?',
  MESSAGE: '๋ˆ„๊ตฌ์—๊ฒŒ ๋ฌธ์ž๋ฅผ ์ „์†กํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?',
} as const;

Line range hint 1-14: ์ปดํฌ๋„ŒํŠธ ๊ตฌ์กฐ ๊ฐœ์„  ์ œ์•ˆ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐœ์„ ์‚ฌํ•ญ์„ ๊ณ ๋ คํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  1. ์—๋Ÿฌ ๋ฐ”์šด๋”๋ฆฌ ์ถ”๊ฐ€
  2. params ํƒ€์ž…์„ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ์ •์˜
  3. ์ปค์Šคํ…€ ํ›…๋“ค์˜ ๋กœ๋”ฉ/์—๋Ÿฌ ์ƒํƒœ ์ฒ˜๋ฆฌ

์˜ˆ์‹œ:

interface ExpoActionPanelProps {
  params: {
    id: number;
    // ํ•„์š”ํ•œ ๋‹ค๋ฅธ ์†์„ฑ๋“ค...
  };
}

// ์—๋Ÿฌ ๋ฐ”์šด๋”๋ฆฌ ์˜ˆ์‹œ
class ExpoActionPanelErrorBoundary extends React.Component {
  // ๊ตฌํ˜„...
}
๐Ÿ“œ Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

๐Ÿ“ฅ Commits

Reviewing files that changed from the base of the PR and between bf9f25c and 78c8c1e.

๐Ÿ“’ Files selected for processing (9)
  • src/app/api/role/route.ts (1 hunks)
  • src/app/api/tokenCheck/route.ts (0 hunks)
  • src/entities/layout/Header/model/useNavigation.ts (1 hunks)
  • src/middleware.ts (1 hunks)
  • src/shared/components/ClientInitializer.tsx (0 hunks)
  • src/shared/model/useRole.ts (1 hunks)
  • src/shared/stores/useStore.ts (0 hunks)
  • src/widgets/expo-detail/ui/ExpoActionPanel/index.tsx (2 hunks)
  • src/widgets/layout/ui/Header/index.tsx (0 hunks)
๐Ÿ’ค Files with no reviewable changes (4)
  • src/widgets/layout/ui/Header/index.tsx
  • src/app/api/tokenCheck/route.ts
  • src/shared/components/ClientInitializer.tsx
  • src/shared/stores/useStore.ts

Comment on lines +3 to +7
export async function GET(request: NextRequest) {
const role = request.headers.get('role') || 'user';

return NextResponse.json({ role });
}
Copy link

Choose a reason for hiding this comment

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

๐Ÿ› ๏ธ Refactor suggestion

์—ญํ•  ๊ฒ€์ฆ ๋ฐ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ ๋กœ์ง ์ถ”๊ฐ€ ํ•„์š”

ํ˜„์žฌ ๊ตฌํ˜„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐœ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค:

  1. ์œ ํšจํ•œ ์—ญํ•  ๊ฐ’ ๊ฒ€์ฆ์ด ์—†์Šต๋‹ˆ๋‹ค
  2. ์š”์ฒญ ์˜ค๋ฅ˜์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค
  3. ์—ญํ•  ๊ฐ’์— ๋Œ€ํ•œ ํƒ€์ž… ์•ˆ์ „์„ฑ์ด ๋ณด์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค:

+const VALID_ROLES = ['user', 'manage'] as const;
+type Role = typeof VALID_ROLES[number];
+
 export async function GET(request: NextRequest) {
-  const role = request.headers.get('role') || 'user';
+  try {
+    const roleHeader = request.headers.get('role');
+    const role = roleHeader || 'user';
+
+    if (!VALID_ROLES.includes(role as Role)) {
+      return NextResponse.json(
+        { error: '์œ ํšจํ•˜์ง€ ์•Š์€ ์—ญํ• ์ž…๋‹ˆ๋‹ค.' },
+        { status: 400 }
+      );
+    }
 
-  return NextResponse.json({ role });
+    return NextResponse.json({ role });
+  } catch (error) {
+    return NextResponse.json(
+      { error: '์„œ๋ฒ„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.' },
+      { status: 500 }
+    );
+  }
 }

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +5 to +19
export const useRole = () => {
const [role, setRole] = useState<string | null>(null);

useEffect(() => {
const fetchRole = async () => {
const response = await fetch('/api/role');
const data = await response.json();
setRole(data.role);
};

fetchRole();
}, []);

return role;
};
Copy link

Choose a reason for hiding this comment

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

๐Ÿ› ๏ธ Refactor suggestion

์—๋Ÿฌ ์ฒ˜๋ฆฌ ๋ฐ ์ƒํƒœ ๊ด€๋ฆฌ ๊ฐœ์„  ํ•„์š”

ํ˜„์žฌ ๊ตฌํ˜„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐœ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค:

  1. ๋„คํŠธ์›Œํฌ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค
  2. ๋กœ๋”ฉ ์ƒํƒœ ๊ด€๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค
  3. ์—๋Ÿฌ ์ƒํƒœ ๊ด€๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ์„ ํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค:

 export const useRole = () => {
   const [role, setRole] = useState<string | null>(null);
+  const [isLoading, setIsLoading] = useState(true);
+  const [error, setError] = useState<Error | null>(null);
 
   useEffect(() => {
     const fetchRole = async () => {
+      setIsLoading(true);
+      setError(null);
+
+      try {
         const response = await fetch('/api/role');
+        if (!response.ok) {
+          throw new Error('์—ญํ• ์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
+        }
         const data = await response.json();
         setRole(data.role);
+      } catch (err) {
+        setError(err instanceof Error ? err : new Error('์•Œ ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.'));
+        setRole(null);
+      } finally {
+        setIsLoading(false);
+      }
     };
 
     fetchRole();
   }, []);
 
-  return role;
+  return { role, isLoading, error };
 };
๐Ÿ“ Committable suggestion

โ€ผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const useRole = () => {
const [role, setRole] = useState<string | null>(null);
useEffect(() => {
const fetchRole = async () => {
const response = await fetch('/api/role');
const data = await response.json();
setRole(data.role);
};
fetchRole();
}, []);
return role;
};
export const useRole = () => {
const [role, setRole] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
const fetchRole = async () => {
setIsLoading(true);
setError(null);
try {
const response = await fetch('/api/role');
if (!response.ok) {
throw new Error('์—ญํ• ์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
}
const data = await response.json();
setRole(data.role);
} catch (err) {
setError(err instanceof Error ? err : new Error('์•Œ ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.'));
setRole(null);
} finally {
setIsLoading(false);
}
};
fetchRole();
}, []);
return { role, isLoading, error };
};

Comment on lines +4 to +20
export function middleware(request: NextRequest) {
const accessToken = request.cookies.get('accessToken');

const requestHeaders = new Headers(request.headers);

if (accessToken) {
requestHeaders.set('role', 'manage');
} else {
requestHeaders.set('role', 'user');
}

return NextResponse.next({
request: {
headers: requestHeaders,
},
});
}
Copy link

Choose a reason for hiding this comment

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

โš ๏ธ Potential issue

ํ† ํฐ ๊ฒ€์ฆ ๋ฐ ์—ญํ•  ํ• ๋‹น ๋กœ์ง ๊ฐœ์„  ํ•„์š”

ํ˜„์žฌ ๊ตฌํ˜„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ณด์•ˆ ๋ฐ ๊ธฐ๋Šฅ์  ๊ฐœ์„ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค:

  1. ์•ก์„ธ์Šค ํ† ํฐ์˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์ด ์—†์Šต๋‹ˆ๋‹ค
  2. ์—ญํ•  ํ• ๋‹น์ด ์ด์ง„์ ์ž…๋‹ˆ๋‹ค (ํ† ํฐ ์žˆ์Œ/์—†์Œ)
  3. ํ† ํฐ ๋งŒ๋ฃŒ ํ™•์ธ์ด ์—†์Šต๋‹ˆ๋‹ค

ํ† ํฐ ๊ฒ€์ฆ ๋ฐ ์—ญํ•  ํ• ๋‹น ๋กœ์ง์„ ๋” ๊ฒฌ๊ณ ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์„ ์ œ์•ˆํ•ฉ๋‹ˆ๋‹ค:

 export function middleware(request: NextRequest) {
-  const accessToken = request.cookies.get('accessToken');
+  const accessToken = request.cookies.get('accessToken')?.value;
   const requestHeaders = new Headers(request.headers);
 
-  if (accessToken) {
-    requestHeaders.set('role', 'manage');
-  } else {
+  try {
+    if (!accessToken) {
+      requestHeaders.set('role', 'user');
+      return NextResponse.next({
+        request: { headers: requestHeaders },
+      });
+    }
+
+    // ํ† ํฐ ๊ฒ€์ฆ ๋กœ์ง ์ถ”๊ฐ€
+    const decodedToken = verifyAccessToken(accessToken);
+    
+    if (decodedToken.role) {
+      requestHeaders.set('role', decodedToken.role);
+    } else {
+      requestHeaders.set('role', 'user');
+    }
+  } catch (error) {
     requestHeaders.set('role', 'user');
   }
 
   return NextResponse.next({
-    request: {
-      headers: requestHeaders,
-    },
+    request: { headers: requestHeaders },
   });
 }

Committable suggestion skipped: line range outside the PR's diff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
โ™ป๏ธ Refactor ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants