Files
Momento/memento-mobile/lib/store.ts
Antigravity d2145f761d mobile: fix navigation (typed routes), extract C tokens to lib/theme.ts
- lib/theme.ts: C design tokens dans fichier dédié (plus d'import circulaire _layout)
- app/_layout.tsx: importe C depuis @/lib/theme, ré-exporte pour compatibilité
- Tous les écrans: import C depuis '@/lib/theme' au lieu de '../_layout'
- Toutes les navigations: router.push({ pathname, params }) au lieu de template strings
  -> Fix réel du bug 'impossible d'ouvrir carnet/note' avec Expo Router v6
- package.json: expo-web-browser ajouté (pour Google OAuth étape suivante)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-29 17:03:14 +00:00

58 lines
1.4 KiB
TypeScript

import { create } from 'zustand'
import { apiFetch, setToken, clearToken, getToken } from '@/lib/api'
import { ENDPOINTS } from '@/lib/config'
interface User {
id: string
name: string | null
email: string
tier: string
}
interface AuthState {
user: User | null
loading: boolean
login: (email: string, password: string) => Promise<void>
loginWithToken: (token: string, user: User) => Promise<void>
logout: () => Promise<void>
restore: () => Promise<void>
}
export const useAuthStore = create<AuthState>((set) => ({
user: null,
loading: true,
login: async (email, password) => {
const res = await fetch(ENDPOINTS.login, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
})
if (!res.ok) {
const data = await res.json()
throw new Error(data.error || 'Identifiants invalides')
}
const { token, user } = await res.json()
await setToken(token)
set({ user })
},
logout: async () => {
await clearToken()
set({ user: null })
},
restore: async () => {
try {
const token = await getToken()
if (!token) { set({ loading: false }); return }
const res = await apiFetch(ENDPOINTS.me)
if (!res.ok) { await clearToken(); set({ loading: false, user: null }); return }
const user = await res.json()
set({ user, loading: false })
} catch {
set({ loading: false, user: null })
}
},
}))