260508_08_ENH: 새로고침 시 로그인 풀리는 문제 수정 중

This commit is contained in:
2026-05-08 11:04:18 +09:00
parent 8c0b19d542
commit c7b0655ec2

View File

@@ -1,71 +1,62 @@
import { Security } from "@/lib/security";
import {clientEnv} from "@/lib/env.client";
import { clientEnv } from "@/lib/env.client";
type StorageValue = any; // 요청대로 any 유지
type StorageValue = any;
// --- 내부 헬퍼 함수들 ---
const getDecryptedValue = (value: string): StorageValue => {
try {
if (!value || value.trim() === "") {
return null;
}
const decryptValue = clientEnv.ENV ==="prod" ? Security.decryptBase64(value) : value;
if (!decryptValue || decryptValue.trim() === "") {
return null;
}
return JSON.parse(decryptValue);
} catch (error) {
// 손상된 데이터인 경우 null 반환
const parseStoredValue = (value: string): StorageValue => {
if (!value || value.trim() === "") {
return null;
}
if (clientEnv.ENV !== "prod") {
return JSON.parse(value);
}
const decryptValue = Security.decryptBase64(value);
if (!decryptValue || decryptValue.trim() === "") {
return null;
}
return JSON.parse(decryptValue);
};
const getEncryptedValue = (value: StorageValue): string => {
const stringifyStorageValue = (value: StorageValue): string => {
const stringifyValue = JSON.stringify(value);
return clientEnv.ENV ==="prod" ? Security.encryptBase64(stringifyValue) : stringifyValue;
return clientEnv.ENV === "prod" ? Security.encryptBase64(stringifyValue) : stringifyValue;
};
// --- LocalStorage 함수들 ---
export const setLocalStorage = (key: string, value: StorageValue): void => {
try {
const encryptValue = getEncryptedValue(value);
localStorage.setItem(key, encryptValue);
localStorage.setItem(key, stringifyStorageValue(value));
} catch (error) {
// 필요하면 로깅 추가
console.warn("[storage] localStorage set failed", { key, error });
}
};
export const getLocalStorage = (key: string): StorageValue => {
try {
const encryptValue: string | null = localStorage.getItem(key);
if (encryptValue === null || encryptValue === "") {
const storedValue = localStorage.getItem(key);
if (storedValue === null || storedValue === "") {
return null;
}
let value: StorageValue = null;
try {
// 암호화된 값 복호화 시도
value = getDecryptedValue(encryptValue);
return parseStoredValue(storedValue);
} catch (decryptError) {
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
try {
value = JSON.parse(encryptValue);
return JSON.parse(storedValue);
} catch (fallbackError) {
// 완전히 실패한 경우 저장된 데이터 제거
localStorage.removeItem(key);
console.warn("[storage] localStorage parse failed; removed corrupted value", {
key,
decryptError,
fallbackError,
});
return null;
}
}
return value;
} catch (error) {
console.warn("[storage] localStorage get failed", { key, error });
return null;
}
};
@@ -78,43 +69,38 @@ export const clearLocalStorage = (): void => {
localStorage.clear();
};
// --- SessionStorage 함수들 ---
// 세션스토리지는 테스트/실제 서버 모두 암호화 없음
export const setSessionStorage = (key: string, value: StorageValue): void => {
try {
const encryptValue = getEncryptedValue(value);
sessionStorage.setItem(key, encryptValue);
sessionStorage.setItem(key, stringifyStorageValue(value));
} catch (error) {
// 필요하면 로깅 추가
console.warn("[storage] sessionStorage set failed", { key, error });
}
};
export const getSessionStorage = (key: string): StorageValue => {
try {
const encryptValue: string | null = sessionStorage.getItem(key);
if (encryptValue === null || encryptValue === "") {
const storedValue = sessionStorage.getItem(key);
if (storedValue === null || storedValue === "") {
return null;
}
let value: StorageValue = null;
try {
// 암호화된 값 복호화 시도
value = getDecryptedValue(encryptValue);
return parseStoredValue(storedValue);
} catch (decryptError) {
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
try {
value = JSON.parse(encryptValue);
return JSON.parse(storedValue);
} catch (fallbackError) {
// 완전히 실패한 경우 저장된 데이터 제거
localStorage.removeItem(key);
sessionStorage.removeItem(key);
console.warn("[storage] sessionStorage parse failed; removed corrupted value", {
key,
decryptError,
fallbackError,
});
return null;
}
}
return value;
} catch (error) {
console.warn("[storage] sessionStorage get failed", { key, error });
return null;
}
};
@@ -125,4 +111,4 @@ export const removeSessionStorage = (key: string): void => {
export const clearSessionStorage = (): void => {
sessionStorage.clear();
};
};