fix: 외부 플랫폼 401 응답으로 앱이 로그아웃되던 문제 수정
This commit is contained in:
1 parent
0d2c4a4a19
commit
70965d3eef
2 files changed
+36
-7
No files matched your search
+13
-7
@@ -1,4 +1,5 @@
|
||||
import { clearSessionStorage } from './storageKeys';
|
||||
import { classifyAppAuthError } from './authError';
|
||||
import {
|
||||
CrawlingResponse,
|
||||
LyricGenerateRequest,
|
||||
@@ -859,18 +860,23 @@ export async function authenticatedFetch(
|
||||
|
||||
// 401 에러 시 에러 코드에 따라 처리
|
||||
if (response.status === 401) {
|
||||
const errorBody = await response.json().catch(() => null);
|
||||
const errorCode = errorBody?.detail?.code;
|
||||
// 호출부가 본문을 다시 읽을 수 있도록 clone 으로 판별한다
|
||||
const errorBody = await response.clone().json().catch(() => null);
|
||||
const authError = classifyAppAuthError(errorBody);
|
||||
|
||||
// 애초에 토큰이 없던 익명 요청(공개 화면)까지 튕기면 안 되므로 별도 처리
|
||||
const isMissingToken = errorCode === 'MISSING_TOKEN';
|
||||
// 앱 로그인 문제가 아닌 401(YouTube/소셜 재연동 필요 등)은 로그아웃하지 않고
|
||||
// 호출부가 code 로 처리하도록 응답을 그대로 돌려준다
|
||||
if (authError === null) {
|
||||
return response;
|
||||
}
|
||||
|
||||
if (errorCode !== 'TOKEN_EXPIRED') {
|
||||
if (authError !== 'expired') {
|
||||
// INVALID_TOKEN 등 갱신으로 해결 불가한 경우 즉시 로그인 이동
|
||||
if (redirectOnAuthFail && !isMissingToken) {
|
||||
// (애초에 토큰이 없던 익명 요청(공개 화면)은 튕기면 안 되므로 missing 은 제외)
|
||||
if (redirectOnAuthFail && authError !== 'missing') {
|
||||
redirectToLogin();
|
||||
}
|
||||
throw new Error(errorCode ?? 'Unauthorized');
|
||||
throw new Error(errorBody?.detail?.code ?? 'Unauthorized');
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// 401 응답이 "앱 로그인(JWT) 문제"인지 구분한다.
|
||||
// 앱 JWT 오류는 { detail: { code, message } } 형태이고,
|
||||
// YouTube/소셜 외부 플랫폼 토큰 오류는 { detail: string, code } 형태라 서로 다르다.
|
||||
// 외부 플랫폼 401까지 로그아웃 처리하면 재연동 안내 대신 앱에서 튕겨 나간다.
|
||||
|
||||
export type AppAuthErrorKind = 'expired' | 'missing' | 'invalid';
|
||||
|
||||
export function classifyAppAuthError(errorBody: unknown): AppAuthErrorKind | null {
|
||||
const detail = (errorBody as { detail?: unknown } | null)?.detail;
|
||||
if (typeof detail !== 'object' || detail === null) return null;
|
||||
|
||||
switch ((detail as { code?: unknown }).code) {
|
||||
case 'TOKEN_EXPIRED':
|
||||
return 'expired';
|
||||
case 'MISSING_TOKEN':
|
||||
return 'missing';
|
||||
case 'INVALID_TOKEN':
|
||||
case 'TOKEN_REVOKED':
|
||||
return 'invalid';
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user