260909_01_ENH: useAuth에서 발생하는 하이드레이션 오류 수정

This commit is contained in:
2026-09-09 10:00:25 +09:00
parent e78994700d
commit ef3bd94874
2 changed files with 32 additions and 19 deletions

View File

@@ -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": {

View File

@@ -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<LoginUser>;
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<AuthState>) }
: initialAuthState;
const isRecord = (value: unknown): value is Record<string, unknown> =>
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<LoginUser>({ 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<AuthState>(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]);