Files
Bahla-Frontend/src/stores/authStore.ts
2026-09-02 11:31:58 +02:00

39 lines
1005 B
TypeScript

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<void>;
signIn: (session: Session) => Promise<void>;
signOut: () => Promise<void>;
};
const KEY = 'bahla.session';
export const useAuth = create<AuthState>((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 });
},
}));