From ef3bd948741ba427db6dea1fd5d57a8a86c88ec9 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Wed, 9 Sep 2026 10:00:25 +0900 Subject: [PATCH] =?UTF-8?q?260909=5F01=5FENH:=20useAuth=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EB=B0=9C=EC=83=9D=ED=95=98=EB=8A=94=20=ED=95=98=EC=9D=B4?= =?UTF-8?q?=EB=93=9C=EB=A0=88=EC=9D=B4=EC=85=98=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- templates/base/src/hooks/useAuth.ts | 49 ++++++++++++++++++----------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 3a7dd79..6633243 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kri/create-next-app", - "version": "0.2.0", + "version": "0.2.1", "description": "Create a KRI standard Next.js application", "type": "module", "bin": { diff --git a/templates/base/src/hooks/useAuth.ts b/templates/base/src/hooks/useAuth.ts index b6e41bd..c6c34ed 100644 --- a/templates/base/src/hooks/useAuth.ts +++ b/templates/base/src/hooks/useAuth.ts @@ -1,35 +1,48 @@ "use client"; import { useCallback } from "react"; +import { useAtom } from "jotai"; import type { LoginUser } from "@kri/common"; -import { createAuthHook, type AuthState as CommonAuthState } from "@kri/common/hooks"; +import type { AuthState as CommonAuthState } from "@kri/common/hooks"; +import { createPersistentAtom } from "@kri/common/jotai"; import { getLocalStorage, removeLocalStorage, setLocalStorage } from "@/store/storage/secure-storage"; import { StorageKeys } from "@/store/storage/storage-keys"; export type AuthState = CommonAuthState; const initialAuthState: AuthState = { user: null }; -const loadInitialAuthState = (): AuthState => { - const stored = getLocalStorage(StorageKeys.AuthState); - return stored && typeof stored === "object" && "user" in stored - ? { ...initialAuthState, ...(stored as Partial) } - : initialAuthState; +const isRecord = (value: unknown): value is Record => + typeof value === "object" && value !== null && !Array.isArray(value); + +const isLoginUser = (value: unknown): value is LoginUser => { + if (!isRecord(value)) return false; + + return ( + typeof value.userId === "string" && + typeof value.userName === "string" && + typeof value.userCmpn === "string" && + typeof value.userTeam === "string" && + typeof value.userRole === "number" + ); }; -const { authAtom, useAuth: useCommonAuth } = createAuthHook({ initialState: loadInitialAuthState() }); -export const authStateAtom = authAtom; +const isPersistedAuthState = (value: unknown): value is AuthState => + isRecord(value) && isLoginUser(value.user); + +/* + * atom은 서버·클라이언트 최초 렌더 모두 initialAuthState로 시작한다. + * 저장소 값은 onMount(마운트 이후 시점)에만 반영되므로 + * 서버 HTML과 클라이언트 최초 렌더가 항상 일치해 하이드레이션 오류가 발생하지 않는다. + */ +export const authStateAtom = createPersistentAtom(StorageKeys.AuthState, initialAuthState, { + storage: { get: getLocalStorage, set: setLocalStorage, remove: removeLocalStorage }, + validate: isPersistedAuthState, + shouldRemove: (value) => value.user === null, + debugLabel: "authState", +}); export function useAuth() { - const { auth, setAuth: setCommonAuth } = useCommonAuth(); - - const setAuth = useCallback((next: AuthState | ((previous: AuthState) => AuthState)) => { - setCommonAuth((previous) => { - const resolved = typeof next === "function" ? next(previous) : next; - if (resolved.user === null) removeLocalStorage(StorageKeys.AuthState); - else setLocalStorage(StorageKeys.AuthState, resolved); - return resolved; - }); - }, [setCommonAuth]); + const [auth, setAuth] = useAtom(authStateAtom); const setFromLoginResponse = useCallback((user: LoginUser) => setAuth({ user }), [setAuth]); const logout = useCallback(() => setAuth(initialAuthState), [setAuth]);