From e93f6c8f4146829c5e9466968f51197af08b8b96 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Fri, 8 May 2026 11:34:10 +0900 Subject: [PATCH] =?UTF-8?q?260508=5F14=5FENH:=20=EC=83=88=EB=A1=9C?= =?UTF-8?q?=EA=B3=A0=EC=B9=A8=20=EC=8B=9C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=ED=92=80=EB=A6=AC=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useAuth.ts | 28 +-------- src/lib/security.ts | 3 +- src/store/jotai/utils.ts | 18 ------ src/store/storage/secure-storage.ts | 89 ++++++++--------------------- 4 files changed, 26 insertions(+), 112 deletions(-) diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index dbdf8dc..13fc675 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -1,39 +1,13 @@ "use client"; -import { useCallback, useEffect } from "react"; +import { useCallback } from "react"; import { useAtom } from "jotai"; import { authStateAtom, type AuthState } from "@/store/jotai/auth"; import type { LoginUser } from "@/types/auth"; -import { getLocalStorage } from "@/store/storage/secure-storage"; -import { StorageKeys } from "@/store/storage/storage-keys"; - -const isAuthDebugEnabled = () => - typeof window !== "undefined" && localStorage.getItem("kfpa:debugAuth") === "1"; export const useAuth = () => { const [auth, setAuth] = useAtom(authStateAtom); - useEffect(() => { - if (isAuthDebugEnabled()) { - console.log("[auth] current atom state", auth); - console.log("[auth] raw storage value", localStorage.getItem(StorageKeys.AuthState)); - } - - if (auth.user != null) return; - - const stored = getLocalStorage(StorageKeys.AuthState) as AuthState | null; - if (isAuthDebugEnabled()) { - console.log("[auth] parsed storage value", stored); - } - - if (stored?.user != null) { - if (isAuthDebugEnabled()) { - console.log("[auth] restoring user from storage", stored.user); - } - setAuth(stored); - } - }, [auth.user, setAuth]); - // 로그인 성공 후 LoginResponse 를 바로 넣어줄 수 있게 const setFromLoginResponse = useCallback( (res: LoginUser) => { diff --git a/src/lib/security.ts b/src/lib/security.ts index d7fe4e2..5f8a2dd 100644 --- a/src/lib/security.ts +++ b/src/lib/security.ts @@ -143,8 +143,7 @@ export class Security { } public static encodeBase64Url(input: WordArray): string { - const base64url = CryptoJS.enc.Base64url.stringify(input); - return this.convertToSpecialChars(base64url); + return CryptoJS.enc.Base64url.stringify(input); } public static decodeBase64Url(input: string): WordArray { diff --git a/src/store/jotai/utils.ts b/src/store/jotai/utils.ts index 2fefca9..60d5bac 100644 --- a/src/store/jotai/utils.ts +++ b/src/store/jotai/utils.ts @@ -65,15 +65,6 @@ export const createLocalStorageAtom = ( }, ); - persistentAtom.onMount = (setAtom) => { - if (typeof window === "undefined" || wasOverridden) return; - - const stored = getLocalStorage(key); - if (stored && typeof stored === "object") { - setAtom(stored as T); - } - }; - return persistentAtom; } @@ -113,14 +104,5 @@ export const createSessionStorageAtom = ( }, ); - persistentAtom.onMount = (setAtom) => { - if (typeof window === "undefined") return; - - const stored = getSessionStorage(key); - if (stored && typeof stored === "object") { - setAtom(stored as T); - } - }; - return persistentAtom; } diff --git a/src/store/storage/secure-storage.ts b/src/store/storage/secure-storage.ts index 670ace1..bf6a2e5 100644 --- a/src/store/storage/secure-storage.ts +++ b/src/store/storage/secure-storage.ts @@ -3,76 +3,48 @@ import { clientEnv } from "@/lib/env.client"; type StorageValue = any; -const parseStoredValue = (value: string): StorageValue => { +const parseStorageValue = (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() === "") { + const rawValue = clientEnv.ENV === "prod" ? Security.decryptBase64(value) : value; + if (!rawValue || rawValue.trim() === "") { return null; } - return JSON.parse(decryptValue); + return JSON.parse(rawValue); }; const stringifyStorageValue = (value: StorageValue): string => { - const stringifyValue = JSON.stringify(value); - return clientEnv.ENV === "prod" ? Security.encryptBase64(stringifyValue) : stringifyValue; + const rawValue = JSON.stringify(value); + return clientEnv.ENV === "prod" ? Security.encryptBase64(rawValue) : rawValue; }; -const isPlainJsonValue = (value: string) => { - const trimmed = value.trim(); - return trimmed.startsWith("{") || trimmed.startsWith("["); -}; - -const isLegacyEncryptedStorageValue = (value: string) => - value.includes("\u200b") || value.includes("\u200c"); - -const shouldMigrateStorageValue = (value: string) => - isPlainJsonValue(value) || isLegacyEncryptedStorageValue(value); - export const setLocalStorage = (key: string, value: StorageValue): void => { try { localStorage.setItem(key, stringifyStorageValue(value)); - } catch (error) { - console.warn("[storage] localStorage set failed", { key, error }); - } + } catch {} }; export const getLocalStorage = (key: string): StorageValue => { try { - const storedValue = localStorage.getItem(key); - if (storedValue === null || storedValue === "") { + const value = localStorage.getItem(key); + if (value === null || value === "") { return null; } try { - const parsedValue = parseStoredValue(storedValue); - if (parsedValue != null && shouldMigrateStorageValue(storedValue)) { - localStorage.setItem(key, stringifyStorageValue(parsedValue)); - } - return parsedValue; - } catch (decryptError) { + return parseStorageValue(value); + } catch { try { - const parsedValue = JSON.parse(storedValue); - localStorage.setItem(key, stringifyStorageValue(parsedValue)); - return parsedValue; - } catch (fallbackError) { - console.warn("[storage] localStorage parse failed", { - key, - decryptError, - fallbackError, - }); + return JSON.parse(value); + } catch { + localStorage.removeItem(key); return null; } } - } catch (error) { - console.warn("[storage] localStorage get failed", { key, error }); + } catch { return null; } }; @@ -88,40 +60,27 @@ export const clearLocalStorage = (): void => { export const setSessionStorage = (key: string, value: StorageValue): void => { try { sessionStorage.setItem(key, stringifyStorageValue(value)); - } catch (error) { - console.warn("[storage] sessionStorage set failed", { key, error }); - } + } catch {} }; export const getSessionStorage = (key: string): StorageValue => { try { - const storedValue = sessionStorage.getItem(key); - if (storedValue === null || storedValue === "") { + const value = sessionStorage.getItem(key); + if (value === null || value === "") { return null; } try { - const parsedValue = parseStoredValue(storedValue); - if (parsedValue != null && shouldMigrateStorageValue(storedValue)) { - sessionStorage.setItem(key, stringifyStorageValue(parsedValue)); - } - return parsedValue; - } catch (decryptError) { + return parseStorageValue(value); + } catch { try { - const parsedValue = JSON.parse(storedValue); - sessionStorage.setItem(key, stringifyStorageValue(parsedValue)); - return parsedValue; - } catch (fallbackError) { - console.warn("[storage] sessionStorage parse failed", { - key, - decryptError, - fallbackError, - }); + return JSON.parse(value); + } catch { + sessionStorage.removeItem(key); return null; } } - } catch (error) { - console.warn("[storage] sessionStorage get failed", { key, error }); + } catch { return null; } };