From 79ca094783b9479f666272f241ddaf496ad7af15 Mon Sep 17 00:00:00 2001 From: happybell80 Date: Mon, 18 Aug 2025 14:09:08 +0900 Subject: [PATCH] =?UTF-8?q?docs:=20=ED=94=84=EB=A1=A0=ED=8A=B8=EC=97=94?= =?UTF-8?q?=EB=93=9C=20localStorage=20user=5Fid=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0=20=EA=B3=BC=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - getMessages API가 default_user로 요청하는 문제 분석 - localStorage에 user_id 저장 누락 발견 - auth-context.tsx 수정으로 해결 - 상태 관리와 영속성 일치 교훈 추가 --- ...18_happybell80_대화히스토리구현.md | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) 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