-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
Walkthrough์ด ํ ๋ฆฌํ์คํธ๋ ์ ํ๋ฆฌ์ผ์ด์
์ ์ญํ (role) ๊ด๋ฆฌ ๋ฐฉ์์ ์ฌ์ค๊ณํฉ๋๋ค. ๊ธฐ์กด์ Zustand ์คํ ์ด ๊ธฐ๋ฐ ์ ๊ทผ ๋ฐฉ์์์ ์๋ก์ด Changes
Sequence DiagramsequenceDiagram
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
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Finishing Touches
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? ๐ชง TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
: ์ญํ ์ํ ์ฒ๋ฆฌ ๋ฐ ํ์ ์์ ์ฑ ๊ฐ์ ํ์ํ์ฌ ๊ตฌํ์๋ ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ด ํ์ํฉ๋๋ค:
- ๋ก๋ฉ ๋ฐ ์๋ฌ ์ํ ์ฒ๋ฆฌ๊ฐ ์์ต๋๋ค
- ์ญํ ๊ฐ์ ๋ํ ํ์ ์์ ์ฑ์ด ๋ณด์ฅ๋์ง ์์ต๋๋ค
- ํ๋์ฝ๋ฉ๋ ์ญํ ๋น๊ต ๋ก์ง์ด ์์ต๋๋ค
๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค:
+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
: ์์ ๋ถ๋ฆฌ ๋ฐ ํ์ ์์ ์ฑ ๊ฐ์ ์ด ํ์ํฉ๋๋ค.์ญํ ๋ณ ๋ฒํผ ๋ ๋๋ง ๋ก์ง์์ ๋ค์ ์ฌํญ๋ค์ ๊ฐ์ ํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค:
- ํ๋์ฝ๋ฉ๋ ๋ฌธ์์ด์ ์์๋ก ๋ถ๋ฆฌ
- ์ญํ ํ์ ์ ๋ช ์์ ์ผ๋ก ์ ์
- ๋ผ์ฐํธ ๊ฒฝ๋ก๋ฅผ ์์๋ก ๋ถ๋ฆฌ
์์ ์ฝ๋:
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
: ์ปดํฌ๋ํธ ๊ตฌ์กฐ ๊ฐ์ ์ ์๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ฌํญ์ ๊ณ ๋ คํด๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค:
- ์๋ฌ ๋ฐ์ด๋๋ฆฌ ์ถ๊ฐ
- params ํ์ ์ ๋ ๊ตฌ์ฒด์ ์ผ๋ก ์ ์
- ์ปค์คํ ํ ๋ค์ ๋ก๋ฉ/์๋ฌ ์ํ ์ฒ๋ฆฌ
์์:
interface ExpoActionPanelProps { params: { id: number; // ํ์ํ ๋ค๋ฅธ ์์ฑ๋ค... }; } // ์๋ฌ ๋ฐ์ด๋๋ฆฌ ์์ class ExpoActionPanelErrorBoundary extends React.Component { // ๊ตฌํ... }
๐ Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
๐ 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
export async function GET(request: NextRequest) { | ||
const role = request.headers.get('role') || 'user'; | ||
|
||
return NextResponse.json({ role }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐ ๏ธ Refactor suggestion
์ญํ ๊ฒ์ฆ ๋ฐ ์ค๋ฅ ์ฒ๋ฆฌ ๋ก์ง ์ถ๊ฐ ํ์
ํ์ฌ ๊ตฌํ์๋ ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ด ํ์ํฉ๋๋ค:
- ์ ํจํ ์ญํ ๊ฐ ๊ฒ์ฆ์ด ์์ต๋๋ค
- ์์ฒญ ์ค๋ฅ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ์์ต๋๋ค
- ์ญํ ๊ฐ์ ๋ํ ํ์ ์์ ์ฑ์ด ๋ณด์ฅ๋์ง ์์ต๋๋ค
๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค:
+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.
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐ ๏ธ Refactor suggestion
์๋ฌ ์ฒ๋ฆฌ ๋ฐ ์ํ ๊ด๋ฆฌ ๊ฐ์ ํ์
ํ์ฌ ๊ตฌํ์๋ ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ด ํ์ํฉ๋๋ค:
- ๋คํธ์ํฌ ์ค๋ฅ ์ฒ๋ฆฌ๊ฐ ์์ต๋๋ค
- ๋ก๋ฉ ์ํ ๊ด๋ฆฌ๊ฐ ์์ต๋๋ค
- ์๋ฌ ์ํ ๊ด๋ฆฌ๊ฐ ์์ต๋๋ค
๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค:
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.
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 }; | |
}; |
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, | ||
}, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ ํฐ ๊ฒ์ฆ ๋ฐ ์ญํ ํ ๋น ๋ก์ง ๊ฐ์ ํ์
ํ์ฌ ๊ตฌํ์๋ ๋ค์๊ณผ ๊ฐ์ ๋ณด์ ๋ฐ ๊ธฐ๋ฅ์ ๊ฐ์ ์ด ํ์ํฉ๋๋ค:
- ์ก์ธ์ค ํ ํฐ์ ์ ํจ์ฑ ๊ฒ์ฆ์ด ์์ต๋๋ค
- ์ญํ ํ ๋น์ด ์ด์ง์ ์ ๋๋ค (ํ ํฐ ์์/์์)
- ํ ํฐ ๋ง๋ฃ ํ์ธ์ด ์์ต๋๋ค
ํ ํฐ ๊ฒ์ฆ ๋ฐ ์ญํ ํ ๋น ๋ก์ง์ ๋ ๊ฒฌ๊ณ ํ๊ฒ ๊ตฌํํ๋ ๊ฒ์ ์ ์ํฉ๋๋ค:
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.
๐ก ๋ฐฐ๊ฒฝ ๋ฐ ๊ฐ์
์ญํ ๊ฒ์ฆ ๋ก์ง ๊ฐ์
๐ ์์ ๋ด์ฉ
middlware๋ฅผ ์ฌ์ฉํ์ฌ role์ ๊ฒ์ฆํจ
middleware์์ ๊ฒ์ฆํ role์ route api์์ ํธ์ถํจ
๐ธ ๊ธฐํ
#71
์ถํ ๋์ฑ ์ข์ ๋ฐฉ๋ฒ์ ์ฐพ์ ํ์๊ฐ ์์ด๋ณด์
Summary by CodeRabbit
์๋ก์ด ๊ธฐ๋ฅ
๋ณ๊ฒฝ ์ฌํญ
๊ธฐํ