Files
sam-react-prod/src/hooks/useUserRole.ts
유병철 55e0791e16 refactor(WEB): Server Action 공통화 및 보안 강화
- executeServerAction 공통 유틸 도입으로 actions.ts 대폭 간소화 (50+개 파일)
- sanitize 유틸 추가 (XSS 방지)
- middleware CSP 헤더 추가 및 Open Redirect 방지
- 프록시 라우트 로깅 개발환경 한정으로 변경
- 프로덕션 불필요 console.log 제거

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 16:14:06 +09:00

36 lines
1.2 KiB
TypeScript

import { useState, useEffect } from 'react';
import { safeJsonParse } from '@/lib/utils';
/**
* 사용자 역할을 관리하는 최적화된 훅
* localStorage 변경 감지 및 자동 업데이트
*/
export function useUserRole() {
const [userRole, setUserRole] = useState<string>(() => {
// SSR-safe: 서버에서는 기본값 반환
if (typeof window === 'undefined') return "CEO";
const userData = safeJsonParse<Record<string, unknown> | null>(localStorage.getItem("user"), null);
return (userData?.role as string) || "CEO";
});
useEffect(() => {
const handleStorageChange = () => {
const userData = safeJsonParse<Record<string, unknown> | null>(localStorage.getItem("user"), null);
const newRole = (userData?.role as string) || "CEO";
setUserRole(newRole);
};
// Listen to custom storage event
window.addEventListener('storage', handleStorageChange);
// Listen to custom role change event
window.addEventListener('roleChanged', handleStorageChange);
return () => {
window.removeEventListener('storage', handleStorageChange);
window.removeEventListener('roleChanged', handleStorageChange);
};
}, []);
return userRole;
}