260702_01_ENH: 리스트 작업 도중 공용 ui를 적용하기 위한 중간 커밋
This commit is contained in:
23
src/store/cookie/client-cookie.ts
Normal file
23
src/store/cookie/client-cookie.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
"use client";
|
||||
|
||||
import {
|
||||
createCookieStore,
|
||||
type CookieSetOptions,
|
||||
} from "@kri/common/browser";
|
||||
import type {
|
||||
CookieKey,
|
||||
} from "./cookie-keys";
|
||||
|
||||
export type {
|
||||
CookieSetOptions,
|
||||
};
|
||||
|
||||
export const cookieStore =
|
||||
createCookieStore<CookieKey>({
|
||||
defaults: {
|
||||
days: 365,
|
||||
path: "/",
|
||||
secure: false,
|
||||
sameSite: "Lax",
|
||||
},
|
||||
});
|
||||
@@ -1,11 +1,7 @@
|
||||
export const CookieKeys = {
|
||||
FontSize: "kri_fontSize",
|
||||
AuthToken: "kri_authToken",
|
||||
// 필요하면 여기 계속 추가 (예: Theme: "kri_theme", ...)
|
||||
} as const;
|
||||
|
||||
export const CookieType: Record<CookieKey, "session" | "persistence-http-only" | "persistence"> = {
|
||||
[CookieKeys.FontSize]: "persistence",
|
||||
};
|
||||
|
||||
|
||||
export type CookieKey = (typeof CookieKeys)[keyof typeof CookieKeys];
|
||||
@@ -1,105 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import type { CookieKey } from "./cookie-keys";
|
||||
|
||||
export type CookieSetOptions = {
|
||||
days?: number; // 만료일: 기본 365일
|
||||
path?: string; // 기본 "/"
|
||||
secure?: boolean;
|
||||
sameSite?: "Lax" | "Strict" | "None";
|
||||
};
|
||||
|
||||
function isBrowser() {
|
||||
return typeof document !== "undefined";
|
||||
}
|
||||
|
||||
function buildCookieString(
|
||||
key: CookieKey,
|
||||
value: string,
|
||||
options: CookieSetOptions = {},
|
||||
): string {
|
||||
const {
|
||||
days = 365,
|
||||
path = "/",
|
||||
secure = false,
|
||||
sameSite = "Lax",
|
||||
} = options;
|
||||
|
||||
let cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
||||
|
||||
// 만료일
|
||||
if (Number.isFinite(days)) {
|
||||
const expires = new Date();
|
||||
expires.setDate(expires.getDate() + days);
|
||||
cookie += `; Expires=${expires.toUTCString()}`;
|
||||
}
|
||||
|
||||
if (path) {
|
||||
cookie += `; Path=${path}`;
|
||||
}
|
||||
|
||||
if (secure) {
|
||||
cookie += "; Secure";
|
||||
}
|
||||
|
||||
if (sameSite) {
|
||||
cookie += `; SameSite=${sameSite}`;
|
||||
}
|
||||
|
||||
return cookie;
|
||||
}
|
||||
|
||||
export const cookieStore = {
|
||||
/**
|
||||
* 문자열 값 가져오기 (없으면 null)
|
||||
*/
|
||||
get(key: CookieKey): string | null {
|
||||
if (!isBrowser()) return null;
|
||||
|
||||
try {
|
||||
const name = encodeURIComponent(key) + "=";
|
||||
const cookies = document.cookie ? document.cookie.split("; ") : [];
|
||||
|
||||
for (const c of cookies) {
|
||||
if (c.startsWith(name)) {
|
||||
const value = c.substring(name.length);
|
||||
return decodeURIComponent(value);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 문자열 값 저장
|
||||
*/
|
||||
set(key: CookieKey, value: string, options?: CookieSetOptions) {
|
||||
if (!isBrowser()) return;
|
||||
|
||||
try {
|
||||
const cookie = buildCookieString(key, value, options);
|
||||
document.cookie = cookie;
|
||||
} catch {
|
||||
// 쿠키 설정 실패는 조용히 무시
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 쿠키 삭제
|
||||
*/
|
||||
remove(key: CookieKey, options?: Omit<CookieSetOptions, "days">) {
|
||||
if (!isBrowser()) return;
|
||||
|
||||
try {
|
||||
document.cookie = buildCookieString(key, "", {
|
||||
...(options ?? {}),
|
||||
days: -1, // 과거로 설정해서 삭제
|
||||
});
|
||||
} catch {
|
||||
// 무시
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -1,22 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { createLocalStorageAtom } from "@/store/jotai/utils";
|
||||
import { StorageKeys } from "@/store/storage/storage-keys";
|
||||
import type { LoginUser } from "@/types/auth";
|
||||
|
||||
export type AuthState = {
|
||||
user: LoginUser | null;
|
||||
seatNumber: string | null;
|
||||
};
|
||||
|
||||
const initialAuthState: AuthState = {
|
||||
user: null,
|
||||
seatNumber: null,
|
||||
};
|
||||
/**
|
||||
* AES 암호화된 localStorage 기반 로그인 상태 Atom
|
||||
*/
|
||||
export const authStateAtom = createLocalStorageAtom<AuthState>(
|
||||
StorageKeys.AuthState,
|
||||
initialAuthState,
|
||||
);
|
||||
export {
|
||||
authStateAtom,
|
||||
type AuthState,
|
||||
} from "@/hooks/useAuth";
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
import { atom } from 'jotai'
|
||||
|
||||
// 사용할 수 있는 DialogId를 타입으로 고정
|
||||
// Dialog추가 할 때만다 추가하기
|
||||
export type DialogId =
|
||||
| 'userUpsertDialog'
|
||||
| 'userUploadDialog'
|
||||
| 'listUploadDialog'
|
||||
| 'listBatchEditDialog'
|
||||
| 'listFilterDialog'
|
||||
| 'projectCreateDialog'
|
||||
| 'resultCodeDialog'
|
||||
| 'resultCodeCopyDialog'
|
||||
| 'listResetDialog'
|
||||
| 'listExcelUploadDialog'
|
||||
| 'listHeaderManageDialog'
|
||||
| 'listDownloadDialog'
|
||||
| 'messageSendDialog'
|
||||
| 'messageHistoryDialog'
|
||||
|
||||
// 현재 열려 있는 다이얼로그 ID 리스트
|
||||
export const openDialogIdsAtom = atom<DialogId[]>([])
|
||||
| "userUpsertDialog"
|
||||
| "userUploadDialog"
|
||||
| "listUploadDialog"
|
||||
| "listBatchEditDialog"
|
||||
| "listFilterDialog"
|
||||
| "projectCreateDialog"
|
||||
| "resultCodeDialog"
|
||||
| "resultCodeCopyDialog"
|
||||
| "listResetDialog"
|
||||
| "listExcelUploadDialog"
|
||||
| "listHeaderManageDialog"
|
||||
| "listDownloadDialog"
|
||||
| "messageSendDialog"
|
||||
| "messageHistoryDialog";
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
"use client";
|
||||
|
||||
import { atom } from "jotai";
|
||||
import {
|
||||
isLoadingAtom,
|
||||
} from "@/hooks/useLoading";
|
||||
|
||||
export const manualLoadingAtom = atom(false);
|
||||
export const queryLoadingAtom = atom(false);
|
||||
export const manualLoadingAtom =
|
||||
isLoadingAtom;
|
||||
|
||||
// 기존 LoadingOverlay가 보는 값: manual || query
|
||||
export const loadingAtom = atom((get) => get(manualLoadingAtom) || get(queryLoadingAtom));
|
||||
export const queryLoadingAtom =
|
||||
atom(false);
|
||||
|
||||
export const loadingAtom =
|
||||
atom((get) => {
|
||||
return (
|
||||
get(isLoadingAtom)
|
||||
|| get(queryLoadingAtom)
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { StorageKeys } from "@/store/storage/storage-keys";
|
||||
import {createSessionStorageAtom} from "@/store/jotai/utils";
|
||||
import { createPersistentAtom } from "@kri/common/jotai";
|
||||
import { sessionJsonStorage } from "@/store/storage/secure-storage";
|
||||
import {SurveyType} from "@/types/project";
|
||||
|
||||
export type SelectedProject = {
|
||||
@@ -22,7 +23,13 @@ export type SelectedProject = {
|
||||
* - 탭/브라우저 닫으면: 삭제
|
||||
*/
|
||||
export const selectedProjectAtom =
|
||||
createSessionStorageAtom<SelectedProject | null>(
|
||||
createPersistentAtom<SelectedProject | null>(
|
||||
StorageKeys.SelectedProject,
|
||||
null,
|
||||
);
|
||||
{
|
||||
storage: sessionJsonStorage,
|
||||
debugLabel: "selectedProjectAtom",
|
||||
},
|
||||
);
|
||||
|
||||
export const selectedProjectHydratedAtom = selectedProjectAtom.hydratedAtom;
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { atom, type PrimitiveAtom } from "jotai";
|
||||
import {
|
||||
setLocalStorage,
|
||||
getLocalStorage,
|
||||
removeLocalStorage,
|
||||
setSessionStorage,
|
||||
getSessionStorage,
|
||||
removeSessionStorage
|
||||
} from "@/store/storage/secure-storage";
|
||||
import {StorageKey} from "@/store/storage/storage-keys";
|
||||
|
||||
const loadInitialState = <T>(storageType: "local"|"session", key: StorageKey, initialValue: T): T => {
|
||||
if (typeof window === "undefined") {
|
||||
// SSR 시에는 그냥 기본값 사용
|
||||
return initialValue;
|
||||
}
|
||||
|
||||
const stored = storageType == "local" ? getLocalStorage(key) : getSessionStorage(key);
|
||||
|
||||
if (stored && typeof stored === "object") {
|
||||
return stored as T;
|
||||
}
|
||||
|
||||
return initialValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* localStorage(AES 암호화) + Jotai 연동용 Atom 생성기
|
||||
* - initialValue: SSR 시/최초 렌더 시 기본값
|
||||
* - 마운트 후 localStorage에서 실제 값 읽어와서 덮어씀
|
||||
*/
|
||||
export const createLocalStorageAtom = <T>(
|
||||
key: StorageKey,
|
||||
initialValue: T,
|
||||
) => {
|
||||
const realInitialValue = loadInitialState("local", key, initialValue);
|
||||
const baseAtom: PrimitiveAtom<T> = atom<T>(realInitialValue);
|
||||
|
||||
// 하이드레이션(저장소 주입)과 사용자 set 경합 방지용 플래그
|
||||
let wasOverridden = false; // 마운트 이후 사용자가 값을 쓴 적이 있는지
|
||||
|
||||
const persistentAtom = atom(
|
||||
(get) => get(baseAtom),
|
||||
(get, set, update: T | ((prev: T) => T)) => {
|
||||
const prev = get(baseAtom);
|
||||
const next =
|
||||
typeof update === "function"
|
||||
? (update as (prev: T) => T)(prev)
|
||||
: update;
|
||||
|
||||
// 메모리 상태 갱신
|
||||
set(baseAtom, next);
|
||||
// 사용자 쓰기 발생 표시 (하이드레이션이 도착해도 무시하도록)
|
||||
wasOverridden = true;
|
||||
|
||||
if (next === null || (next as unknown) === undefined) {
|
||||
// 암호화 localStorage 및 캐시 제거
|
||||
removeLocalStorage(key);
|
||||
} else {
|
||||
// 동기 API로 캐시를 먼저 기록하고 암호화 저장은 백그라운드 처리
|
||||
setLocalStorage(key, next);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
return persistentAtom;
|
||||
}
|
||||
|
||||
/**
|
||||
* sessionStorage + Jotai 연동용 Atom 생성기
|
||||
* - 탭 단위로 유지, 탭/브라우저 닫히면 삭제
|
||||
* - 새로고침 시 유지
|
||||
*/
|
||||
export const createSessionStorageAtom = <T>(
|
||||
key: StorageKey,
|
||||
initialValue: T,
|
||||
) => {
|
||||
const realInitialValue = loadInitialState("session", key, initialValue);
|
||||
const baseAtom: PrimitiveAtom<T> = atom<T>(realInitialValue);
|
||||
|
||||
const persistentAtom = atom(
|
||||
(get) => get(baseAtom),
|
||||
(get, set, update: T | ((prev: T) => T)) => {
|
||||
const prev = get(baseAtom);
|
||||
const next =
|
||||
typeof update === "function"
|
||||
? (update as (prev: T) => T)(prev)
|
||||
: update;
|
||||
|
||||
set(baseAtom, next);
|
||||
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
if (next === null || (next as unknown) === undefined) {
|
||||
removeSessionStorage(key)
|
||||
} else {
|
||||
setSessionStorage(key, next);
|
||||
}
|
||||
} catch {
|
||||
// sessionStorage 에러는 조용히 무시
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
return persistentAtom;
|
||||
}
|
||||
@@ -1,128 +1,65 @@
|
||||
import { Security } from "@/lib/security";
|
||||
import {clientEnv} from "@/lib/env.client";
|
||||
"use client";
|
||||
|
||||
type StorageValue = any; // 요청대로 any 유지
|
||||
import {
|
||||
createJsonStorage,
|
||||
type JsonStorageTransform,
|
||||
} from "@kri/common/browser";
|
||||
import {
|
||||
clientEnv,
|
||||
} from "@/lib/env.client";
|
||||
import {
|
||||
Security,
|
||||
} from "@/lib/security";
|
||||
|
||||
// --- 내부 헬퍼 함수들 ---
|
||||
const securityTransform: JsonStorageTransform = {
|
||||
encode: (value) => {
|
||||
return Security.encryptBase64(value);
|
||||
},
|
||||
|
||||
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;
|
||||
}
|
||||
decode: (value) => {
|
||||
return Security.decryptBase64(value);
|
||||
},
|
||||
};
|
||||
|
||||
const getEncryptedValue = (value: StorageValue): string => {
|
||||
const stringifyValue = JSON.stringify(value);
|
||||
return clientEnv.ENV ==="prod" ? Security.encryptBase64(stringifyValue) : stringifyValue;
|
||||
const createOptions = (storage: "local" | "session") => {
|
||||
return {
|
||||
storage,
|
||||
fallbackToPlainJson: true,
|
||||
|
||||
...(clientEnv.ENV === "prod"
|
||||
? {
|
||||
transform: securityTransform,
|
||||
}
|
||||
: {}),
|
||||
} as const;
|
||||
};
|
||||
|
||||
export const localJsonStorage =
|
||||
createJsonStorage(createOptions("local"));
|
||||
|
||||
// --- LocalStorage 함수들 ---
|
||||
export const sessionJsonStorage =
|
||||
createJsonStorage(createOptions("session"));
|
||||
|
||||
export const setLocalStorage = (key: string, value: StorageValue): void => {
|
||||
try {
|
||||
const encryptValue = getEncryptedValue(value);
|
||||
localStorage.setItem(key, encryptValue);
|
||||
} catch (error) {
|
||||
// 필요하면 로깅 추가
|
||||
}
|
||||
};
|
||||
export const setLocalStorage =
|
||||
localJsonStorage.set;
|
||||
|
||||
export const getLocalStorage = (key: string): StorageValue => {
|
||||
try {
|
||||
const encryptValue: string | null = localStorage.getItem(key);
|
||||
if (encryptValue === null || encryptValue === "") {
|
||||
return null;
|
||||
}
|
||||
export const getLocalStorage =
|
||||
localJsonStorage.get;
|
||||
|
||||
let value: StorageValue = null;
|
||||
export const removeLocalStorage =
|
||||
localJsonStorage.remove;
|
||||
|
||||
try {
|
||||
// 암호화된 값 복호화 시도
|
||||
value = getDecryptedValue(encryptValue);
|
||||
} catch (decryptError) {
|
||||
// 복호화 실패 시 평문으로 파싱 시도 (fallback)
|
||||
try {
|
||||
value = JSON.parse(encryptValue);
|
||||
} catch (fallbackError) {
|
||||
// 완전히 실패한 경우 저장된 데이터 제거
|
||||
localStorage.removeItem(key);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
export const clearLocalStorage =
|
||||
localJsonStorage.clear;
|
||||
|
||||
return value;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
export const setSessionStorage =
|
||||
sessionJsonStorage.set;
|
||||
|
||||
export const removeLocalStorage = (key: string): void => {
|
||||
localStorage.removeItem(key);
|
||||
};
|
||||
export const getSessionStorage =
|
||||
sessionJsonStorage.get;
|
||||
|
||||
export const clearLocalStorage = (): void => {
|
||||
localStorage.clear();
|
||||
};
|
||||
export const removeSessionStorage =
|
||||
sessionJsonStorage.remove;
|
||||
|
||||
// --- 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();
|
||||
};
|
||||
export const clearSessionStorage =
|
||||
sessionJsonStorage.clear;
|
||||
|
||||
Reference in New Issue
Block a user