- Harness: kintex-mobile-dev agent + kintex-mobile-orchestrator skill (WISE mobile ref, Stitch-first design rule, dual app targets B2B/B2C) - design.md v2.1: full 84-screen inventory (web 51 / admin 10 / public 8 / mobile 15) with Stitch prompts incl. ticketing (SCR-P7/P8, M14/M15) - PLANNING v3.1: unified account + split signup tracks (2FA required for staff, light signup/guest for visitors), one codebase / two app targets - Deliverables: dev plan (21s), user/operator/developer guides (17/14/15s), program spec (44s, 65 programs, 8 flowcharts), DA (DB design 14s + table spec xlsx 35 tables/299 cols) - Benchmark: ticketing-app-benchmark.md (7 apps) -> IMPLEMENTATION_BACKLOG Phase F (14 items) - Stitch: 23 generated screens saved (mobile 10, admin 6, web core 5, ticket 2) - mobile/: Expo scaffold (SDK 51, expo-router, secure store JWT) - frontend: SCR-13~17 QA fixes, icons.tsx, kintexEvents, V10 seed migration - ci/: KINTEX CI logo assets Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
/*
|
|
* 인증 컨텍스트 — JWT를 secure-store에 영속, 앱 부팅 시 복원.
|
|
* 활성 워크스페이스(행사) 선택 상태 보관. 비밀번호는 저장하지 않는다.
|
|
*/
|
|
import React, {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
useState,
|
|
} from 'react';
|
|
import { setAccessToken } from '../lib/api';
|
|
import { secureDelete, secureGet, secureSet } from '../lib/secureStore';
|
|
import type { AuthUser, LoginResponse, WorkspaceDto } from '../lib/types';
|
|
|
|
const TOKEN_KEY = 'kintex.accessToken';
|
|
const USER_KEY = 'kintex.user';
|
|
|
|
interface AuthState {
|
|
ready: boolean;
|
|
token: string | null;
|
|
user: AuthUser | null;
|
|
workspaces: WorkspaceDto[];
|
|
activeEventId: string | null;
|
|
}
|
|
|
|
interface AuthContextValue extends AuthState {
|
|
activeWorkspace: WorkspaceDto | null;
|
|
signIn: (res: LoginResponse) => Promise<void>;
|
|
signOut: () => Promise<void>;
|
|
selectWorkspace: (eventId: string) => void;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextValue | undefined>(undefined);
|
|
|
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|
const [state, setState] = useState<AuthState>({
|
|
ready: false,
|
|
token: null,
|
|
user: null,
|
|
workspaces: [],
|
|
activeEventId: null,
|
|
});
|
|
|
|
// 부팅 시 토큰 복원
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
(async () => {
|
|
const token = await secureGet(TOKEN_KEY);
|
|
const userRaw = await secureGet(USER_KEY);
|
|
if (cancelled) return;
|
|
const user = userRaw ? (JSON.parse(userRaw) as AuthUser) : null;
|
|
if (token) setAccessToken(token);
|
|
setState((s) => ({ ...s, ready: true, token, user }));
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, []);
|
|
|
|
const signIn = useCallback(async (res: LoginResponse) => {
|
|
setAccessToken(res.accessToken);
|
|
await secureSet(TOKEN_KEY, res.accessToken);
|
|
await secureSet(USER_KEY, JSON.stringify(res.user));
|
|
setState((s) => ({
|
|
...s,
|
|
token: res.accessToken,
|
|
user: res.user,
|
|
workspaces: res.workspaces ?? [],
|
|
activeEventId: res.workspaces?.[0]?.eventId ?? null,
|
|
}));
|
|
}, []);
|
|
|
|
const signOut = useCallback(async () => {
|
|
setAccessToken(null);
|
|
await secureDelete(TOKEN_KEY);
|
|
await secureDelete(USER_KEY);
|
|
setState((s) => ({
|
|
...s,
|
|
token: null,
|
|
user: null,
|
|
workspaces: [],
|
|
activeEventId: null,
|
|
}));
|
|
}, []);
|
|
|
|
const selectWorkspace = useCallback((eventId: string) => {
|
|
setState((s) => ({ ...s, activeEventId: eventId }));
|
|
}, []);
|
|
|
|
const value = useMemo<AuthContextValue>(() => {
|
|
const activeWorkspace =
|
|
state.workspaces.find((w) => w.eventId === state.activeEventId) ?? null;
|
|
return { ...state, activeWorkspace, signIn, signOut, selectWorkspace };
|
|
}, [state, signIn, signOut, selectWorkspace]);
|
|
|
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
|
}
|
|
|
|
export function useAuth(): AuthContextValue {
|
|
const ctx = useContext(AuthContext);
|
|
if (!ctx) throw new Error('useAuth must be used within AuthProvider');
|
|
return ctx;
|
|
}
|