Skip to content

Commit a20f229

Browse files
committed
코드방송 방코드/자동로그아웃 이슈해결
1 parent af47fca commit a20f229

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/api/globalFetch.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,33 @@ const originalFetch = window.fetch;
77

88
// 2. window.fetch 함수를 우리의 감시 기능이 추가된 새 함수로 덮어씁니다.
99
window.fetch = async (...args) => {
10+
const [resource] = args;
11+
const request = resource instanceof Request ? resource : null;
12+
const rawUrl = request ? request.url : (typeof resource === "string" ? resource : "");
13+
14+
let pathname = "";
15+
if (typeof rawUrl === "string" && rawUrl) {
16+
try {
17+
pathname = new URL(rawUrl, window.location.origin).pathname;
18+
} catch (error) {
19+
pathname = rawUrl;
20+
}
21+
}
22+
1023
// 3. 백업해둔 원래 fetch 함수를 호출하여 실제 API 요청을 보냅니다.
1124
const response = await originalFetch(...args);
1225

26+
const isJoinParticipantsRequest =
27+
response.status === 403 &&
28+
typeof pathname === "string" &&
29+
pathname.includes("/api/collab/rooms/") &&
30+
pathname.endsWith("/participants");
31+
1332
// 4. 응답을 받은 후, 인증 실패(401/403)가 발생했다면
1433
if ((response.status === 401 || response.status === 403) && localStorage.getItem("token")) {
34+
if (isJoinParticipantsRequest) {
35+
return response;
36+
}
1537
localStorage.removeItem("token");
1638
localStorage.removeItem("username");
1739
localStorage.removeItem("userId");

src/components/codecast/codecastlive/CodecastLive.jsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@ async function joinRoomApi(roomId, token) {
4242
credentials: 'include',
4343
});
4444

45-
if (!res.ok) {
46-
const text = await res.text().catch(() => '');
47-
if (res.status === 409) return { status: 'already_joined' };
48-
throw new Error(text || `HTTP ${res.status}`);
45+
if (res.ok) {
46+
const data = await res.json().catch(() => null);
47+
if (data && typeof data === 'object') {
48+
return data.status ? data : { status: 'success', ...data };
49+
}
50+
return { status: 'success' };
51+
}
52+
53+
const text = await res.text().catch(() => '');
54+
if (res.status === 409) return { status: 'already_joined', message: text };
55+
if (res.status === 404 || res.status === 403) {
56+
return { status: 'not_found', message: text || '존재하지 않는 방송 코드입니다.' };
4957
}
50-
return await res.json().catch(() => ({ status: 'success' }));
58+
throw new Error(text || `HTTP ${res.status}`);
5159
}
5260

5361

@@ -556,8 +564,13 @@ export default function CodecastLive({ isDark }) {
556564
try {
557565
if (token && room.id) {
558566
console.log('[API] Joining room via REST API:', room.id);
559-
await joinRoomApi(room.id, token);
560-
console.log('[API] Room joined successfully or already registered.');
567+
const joinResult = await joinRoomApi(room.id, token);
568+
if (joinResult?.status === 'not_found') {
569+
alert(joinResult.message || '존재하지 않는 방송 코드입니다.');
570+
navigate('/broadcast', { replace: true });
571+
return;
572+
}
573+
console.log('[API] Room joined successfully or already registered.', joinResult);
561574
}
562575

563576
console.log('[WS] effect start', { roomId: room.id, hasToken: !!token });
@@ -608,7 +621,7 @@ export default function CodecastLive({ isDark }) {
608621
return () => {
609622
unsubs.forEach((u) => u?.());
610623
};
611-
}, [room.id, token, connect, subscribeSystem]);
624+
}, [room.id, token, connect, subscribeSystem, navigate]);
612625

613626
useEffect(() => {
614627
if (!room.id || !sessionId) return;

0 commit comments

Comments
 (0)