260123_ENH : Layout가지 완성된 최초 commit
This commit is contained in:
128
src/store/storage/secure-storage.ts
Normal file
128
src/store/storage/secure-storage.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { Security } from "@/lib/security";
|
||||
import {clientEnv} from "@/lib/env.client";
|
||||
|
||||
type StorageValue = any; // 요청대로 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 반환
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getEncryptedValue = (value: StorageValue): string => {
|
||||
const stringifyValue = JSON.stringify(value);
|
||||
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);
|
||||
} catch (error) {
|
||||
// 필요하면 로깅 추가
|
||||
}
|
||||
};
|
||||
|
||||
export const getLocalStorage = (key: string): StorageValue => {
|
||||
try {
|
||||
const encryptValue: string | null = localStorage.getItem(key);
|
||||
if (encryptValue === null || encryptValue === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
let value: StorageValue = null;
|
||||
|
||||
try {
|
||||
// 암호화된 값 복호화 시도
|
||||
value = getDecryptedValue(encryptValue);
|
||||
} catch (decryptError) {
|
||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
||||
try {
|
||||
value = JSON.parse(encryptValue);
|
||||
} catch (fallbackError) {
|
||||
// 완전히 실패한 경우 저장된 데이터 제거
|
||||
localStorage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const removeLocalStorage = (key: string): void => {
|
||||
localStorage.removeItem(key);
|
||||
};
|
||||
|
||||
export const clearLocalStorage = (): void => {
|
||||
localStorage.clear();
|
||||
};
|
||||
|
||||
// --- SessionStorage 함수들 ---
|
||||
|
||||
// 세션스토리지는 테스트/실제 서버 모두 암호화 없음
|
||||
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
||||
try {
|
||||
const encryptValue = getEncryptedValue(value);
|
||||
sessionStorage.setItem(key, encryptValue);
|
||||
} catch (error) {
|
||||
// 필요하면 로깅 추가
|
||||
}
|
||||
};
|
||||
|
||||
export const getSessionStorage = (key: string): StorageValue => {
|
||||
try {
|
||||
const encryptValue: string | null = sessionStorage.getItem(key);
|
||||
if (encryptValue === null || encryptValue === "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
let value: StorageValue = null;
|
||||
|
||||
try {
|
||||
// 암호화된 값 복호화 시도
|
||||
value = getDecryptedValue(encryptValue);
|
||||
} catch (decryptError) {
|
||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
||||
try {
|
||||
value = JSON.parse(encryptValue);
|
||||
} catch (fallbackError) {
|
||||
// 완전히 실패한 경우 저장된 데이터 제거
|
||||
localStorage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export const removeSessionStorage = (key: string): void => {
|
||||
sessionStorage.removeItem(key);
|
||||
};
|
||||
|
||||
export const clearSessionStorage = (): void => {
|
||||
sessionStorage.clear();
|
||||
};
|
||||
11
src/store/storage/storage-keys.ts
Normal file
11
src/store/storage/storage-keys.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export const StorageKeys = {
|
||||
AuthState: "kri:authState",
|
||||
//SidebarCollapsed: "kri:sidebarCollapsed",
|
||||
// 필요하면 여기 계속 추가
|
||||
} as const;
|
||||
|
||||
export const StorageType: Record<StorageKey, "local" | "session"> = {
|
||||
[StorageKeys.AuthState]: "local",
|
||||
};
|
||||
|
||||
export type StorageKey = (typeof StorageKeys)[keyof typeof StorageKeys];
|
||||
Reference in New Issue
Block a user