import * as SecureStore from 'expo-secure-store'; import { create } from 'zustand'; export type Role = 'user' | 'org'; export type Session = { userId: string; token: string; role: Role; } type AuthState = { session: Session | null; hydrated: boolean; // Le disque a été lu, la réponse est fiable hydrate: () => Promise; signIn: (session: Session) => Promise; signOut: () => Promise; }; const KEY = 'bahla.session'; export const useAuth = create((set) => ({ session: null, hydrated: false, hydrate: async () => { const session = await SecureStore.getItemAsync(KEY); set({ session: session ? JSON.parse(session) : null, hydrated: true }); }, signIn: async (s: Session) => { await SecureStore.setItemAsync(KEY, JSON.stringify(s)); set({ session: s }); }, signOut: async () => { await SecureStore.deleteItemAsync(KEY); set({ session: null }); }, }));