260508_14_ENH: 새로고침 시 로그인 풀리는 문제 수정 중
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -65,15 +65,6 @@ export const createLocalStorageAtom = <T>(
|
||||
},
|
||||
);
|
||||
|
||||
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 = <T>(
|
||||
},
|
||||
);
|
||||
|
||||
persistentAtom.onMount = (setAtom) => {
|
||||
if (typeof window === "undefined") return;
|
||||
|
||||
const stored = getSessionStorage(key);
|
||||
if (stored && typeof stored === "object") {
|
||||
setAtom(stored as T);
|
||||
}
|
||||
};
|
||||
|
||||
return persistentAtom;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user