260508_14_ENH: 새로고침 시 로그인 풀리는 문제 수정 중
This commit is contained in:
@@ -1,39 +1,13 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect } from "react";
|
import { useCallback } from "react";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { authStateAtom, type AuthState } from "@/store/jotai/auth";
|
import { authStateAtom, type AuthState } from "@/store/jotai/auth";
|
||||||
import type { LoginUser } from "@/types/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 = () => {
|
export const useAuth = () => {
|
||||||
const [auth, setAuth] = useAtom(authStateAtom);
|
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 를 바로 넣어줄 수 있게
|
// 로그인 성공 후 LoginResponse 를 바로 넣어줄 수 있게
|
||||||
const setFromLoginResponse = useCallback(
|
const setFromLoginResponse = useCallback(
|
||||||
(res: LoginUser) => {
|
(res: LoginUser) => {
|
||||||
|
|||||||
@@ -143,8 +143,7 @@ export class Security {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static encodeBase64Url(input: WordArray): string {
|
public static encodeBase64Url(input: WordArray): string {
|
||||||
const base64url = CryptoJS.enc.Base64url.stringify(input);
|
return CryptoJS.enc.Base64url.stringify(input);
|
||||||
return this.convertToSpecialChars(base64url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static decodeBase64Url(input: string): WordArray {
|
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;
|
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;
|
return persistentAtom;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,76 +3,48 @@ import { clientEnv } from "@/lib/env.client";
|
|||||||
|
|
||||||
type StorageValue = any;
|
type StorageValue = any;
|
||||||
|
|
||||||
const parseStoredValue = (value: string): StorageValue => {
|
const parseStorageValue = (value: string): StorageValue => {
|
||||||
if (!value || value.trim() === "") {
|
if (!value || value.trim() === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clientEnv.ENV !== "prod") {
|
const rawValue = clientEnv.ENV === "prod" ? Security.decryptBase64(value) : value;
|
||||||
return JSON.parse(value);
|
if (!rawValue || rawValue.trim() === "") {
|
||||||
}
|
|
||||||
|
|
||||||
const decryptValue = Security.decryptBase64(value);
|
|
||||||
if (!decryptValue || decryptValue.trim() === "") {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return JSON.parse(decryptValue);
|
return JSON.parse(rawValue);
|
||||||
};
|
};
|
||||||
|
|
||||||
const stringifyStorageValue = (value: StorageValue): string => {
|
const stringifyStorageValue = (value: StorageValue): string => {
|
||||||
const stringifyValue = JSON.stringify(value);
|
const rawValue = JSON.stringify(value);
|
||||||
return clientEnv.ENV === "prod" ? Security.encryptBase64(stringifyValue) : stringifyValue;
|
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 => {
|
export const setLocalStorage = (key: string, value: StorageValue): void => {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(key, stringifyStorageValue(value));
|
localStorage.setItem(key, stringifyStorageValue(value));
|
||||||
} catch (error) {
|
} catch {}
|
||||||
console.warn("[storage] localStorage set failed", { key, error });
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getLocalStorage = (key: string): StorageValue => {
|
export const getLocalStorage = (key: string): StorageValue => {
|
||||||
try {
|
try {
|
||||||
const storedValue = localStorage.getItem(key);
|
const value = localStorage.getItem(key);
|
||||||
if (storedValue === null || storedValue === "") {
|
if (value === null || value === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parsedValue = parseStoredValue(storedValue);
|
return parseStorageValue(value);
|
||||||
if (parsedValue != null && shouldMigrateStorageValue(storedValue)) {
|
} catch {
|
||||||
localStorage.setItem(key, stringifyStorageValue(parsedValue));
|
|
||||||
}
|
|
||||||
return parsedValue;
|
|
||||||
} catch (decryptError) {
|
|
||||||
try {
|
try {
|
||||||
const parsedValue = JSON.parse(storedValue);
|
return JSON.parse(value);
|
||||||
localStorage.setItem(key, stringifyStorageValue(parsedValue));
|
} catch {
|
||||||
return parsedValue;
|
localStorage.removeItem(key);
|
||||||
} catch (fallbackError) {
|
|
||||||
console.warn("[storage] localStorage parse failed", {
|
|
||||||
key,
|
|
||||||
decryptError,
|
|
||||||
fallbackError,
|
|
||||||
});
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.warn("[storage] localStorage get failed", { key, error });
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -88,40 +60,27 @@ export const clearLocalStorage = (): void => {
|
|||||||
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
export const setSessionStorage = (key: string, value: StorageValue): void => {
|
||||||
try {
|
try {
|
||||||
sessionStorage.setItem(key, stringifyStorageValue(value));
|
sessionStorage.setItem(key, stringifyStorageValue(value));
|
||||||
} catch (error) {
|
} catch {}
|
||||||
console.warn("[storage] sessionStorage set failed", { key, error });
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSessionStorage = (key: string): StorageValue => {
|
export const getSessionStorage = (key: string): StorageValue => {
|
||||||
try {
|
try {
|
||||||
const storedValue = sessionStorage.getItem(key);
|
const value = sessionStorage.getItem(key);
|
||||||
if (storedValue === null || storedValue === "") {
|
if (value === null || value === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parsedValue = parseStoredValue(storedValue);
|
return parseStorageValue(value);
|
||||||
if (parsedValue != null && shouldMigrateStorageValue(storedValue)) {
|
} catch {
|
||||||
sessionStorage.setItem(key, stringifyStorageValue(parsedValue));
|
|
||||||
}
|
|
||||||
return parsedValue;
|
|
||||||
} catch (decryptError) {
|
|
||||||
try {
|
try {
|
||||||
const parsedValue = JSON.parse(storedValue);
|
return JSON.parse(value);
|
||||||
sessionStorage.setItem(key, stringifyStorageValue(parsedValue));
|
} catch {
|
||||||
return parsedValue;
|
sessionStorage.removeItem(key);
|
||||||
} catch (fallbackError) {
|
|
||||||
console.warn("[storage] sessionStorage parse failed", {
|
|
||||||
key,
|
|
||||||
decryptError,
|
|
||||||
fallbackError,
|
|
||||||
});
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch {
|
||||||
console.warn("[storage] sessionStorage get failed", { key, error });
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user