feat : Structure de base
This commit is contained in:
39
src/stores/authStore.ts
Normal file
39
src/stores/authStore.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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 });
|
||||
},
|
||||
}));
|
||||
Reference in New Issue
Block a user