260508_08_ENH: 새로고침 시 로그인 풀리는 문제 수정 중
This commit is contained in:
@@ -1,71 +1,62 @@
|
|||||||
import { Security } from "@/lib/security";
|
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 parseStoredValue = (value: string): StorageValue => {
|
||||||
|
if (!value || value.trim() === "") {
|
||||||
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 반환
|
|
||||||
return null;
|
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);
|
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 => {
|
export const setLocalStorage = (key: string, value: StorageValue): void => {
|
||||||
try {
|
try {
|
||||||
const encryptValue = getEncryptedValue(value);
|
localStorage.setItem(key, stringifyStorageValue(value));
|
||||||
localStorage.setItem(key, encryptValue);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 필요하면 로깅 추가
|
console.warn("[storage] localStorage set failed", { key, error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getLocalStorage = (key: string): StorageValue => {
|
export const getLocalStorage = (key: string): StorageValue => {
|
||||||
try {
|
try {
|
||||||
const encryptValue: string | null = localStorage.getItem(key);
|
const storedValue = localStorage.getItem(key);
|
||||||
if (encryptValue === null || encryptValue === "") {
|
if (storedValue === null || storedValue === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: StorageValue = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 암호화된 값 복호화 시도
|
return parseStoredValue(storedValue);
|
||||||
value = getDecryptedValue(encryptValue);
|
|
||||||
} catch (decryptError) {
|
} catch (decryptError) {
|
||||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
|
||||||
try {
|
try {
|
||||||
value = JSON.parse(encryptValue);
|
return JSON.parse(storedValue);
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
// 완전히 실패한 경우 저장된 데이터 제거
|
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
|
console.warn("[storage] localStorage parse failed; removed corrupted value", {
|
||||||
|
key,
|
||||||
|
decryptError,
|
||||||
|
fallbackError,
|
||||||
|
});
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.warn("[storage] localStorage get failed", { key, error });
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -78,43 +69,38 @@ export const clearLocalStorage = (): void => {
|
|||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- SessionStorage 함수들 ---
|
|
||||||
|
|
||||||
// 세션스토리지는 테스트/실제 서버 모두 암호화 없음
|
|
||||||
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
||||||
try {
|
try {
|
||||||
const encryptValue = getEncryptedValue(value);
|
sessionStorage.setItem(key, stringifyStorageValue(value));
|
||||||
sessionStorage.setItem(key, encryptValue);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 필요하면 로깅 추가
|
console.warn("[storage] sessionStorage set failed", { key, error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSessionStorage = (key: string): StorageValue => {
|
export const getSessionStorage = (key: string): StorageValue => {
|
||||||
try {
|
try {
|
||||||
const encryptValue: string | null = sessionStorage.getItem(key);
|
const storedValue = sessionStorage.getItem(key);
|
||||||
if (encryptValue === null || encryptValue === "") {
|
if (storedValue === null || storedValue === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let value: StorageValue = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 암호화된 값 복호화 시도
|
return parseStoredValue(storedValue);
|
||||||
value = getDecryptedValue(encryptValue);
|
|
||||||
} catch (decryptError) {
|
} catch (decryptError) {
|
||||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
|
||||||
try {
|
try {
|
||||||
value = JSON.parse(encryptValue);
|
return JSON.parse(storedValue);
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
// 완전히 실패한 경우 저장된 데이터 제거
|
sessionStorage.removeItem(key);
|
||||||
localStorage.removeItem(key);
|
console.warn("[storage] sessionStorage parse failed; removed corrupted value", {
|
||||||
|
key,
|
||||||
|
decryptError,
|
||||||
|
fallbackError,
|
||||||
|
});
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.warn("[storage] sessionStorage get failed", { key, error });
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -125,4 +111,4 @@ export const removeSessionStorage = (key: string): void => {
|
|||||||
|
|
||||||
export const clearSessionStorage = (): void => {
|
export const clearSessionStorage = (): void => {
|
||||||
sessionStorage.clear();
|
sessionStorage.clear();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user