diff --git a/troubleshooting/250818_happybell80_대화히스토리구현.md b/troubleshooting/250818_happybell80_대화히스토리구현.md index d66d645..72da213 100644 --- a/troubleshooting/250818_happybell80_대화히스토리구현.md +++ b/troubleshooting/250818_happybell80_대화히스토리구현.md @@ -219,6 +219,44 @@ async def resolve_username(user_id: str) -> str: - 5단계 우선순위로 안정적 해석 - 비동기 처리로 성능 유지 +## 오후 2시 07분 - 프론트엔드 localStorage 문제 해결 + +### 문제 발견 +- `getMessages` API가 계속 'default_user'로 요청 +- 실제 데이터는 `rb10508_test_happybell80_episodic`에 있음 +- localStorage.getItem('user_id')가 null 반환 + +### 근본 원인 +```javascript +// src/services/robing-api.ts +const userId = localStorage.getItem('user_id') || 'default_user'; // 항상 default_user + +// src/contexts/auth-context.tsx +setUser({ + id: username, // username 설정하지만 + // localStorage에 저장하지 않음! +}); +``` + +### 해결 +```javascript +// auth-context.tsx 수정 +// 1. 토큰 복원 시 +localStorage.setItem('user_id', username); + +// 2. 로그인 성공 시 +const username = data.username || data.user_id || 'unknown'; +localStorage.setItem('user_id', username); + +// 3. 로그아웃 시 +localStorage.removeItem('user_id'); +``` + +### 확인 사항 +- UUID가 아닌 username (예: "happybell80") 사용 +- localStorage에 올바른 user_id 저장 +- API 요청 시 X-User-Id 헤더에 실제 username 전송 + ## 교훈 (추가) ### 5. **User ID 체계 통일 필수** @@ -247,4 +285,9 @@ INSERT INTO users (id) VALUES (gen_random_uuid()); CREATE TABLE users ( id UUID PRIMARY KEY DEFAULT gen_random_uuid() ); -``` \ No newline at end of file +``` + +### 9. **상태 관리와 영속성 일치 필수** +- React state에 저장한 값은 localStorage에도 동기화 +- API 요청 시 사용하는 값은 반드시 영속 저장소에 보관 +- 로그인/로그아웃 시 모든 관련 데이터 정리 \ No newline at end of file