Spaces:
Paused
Paused
Download apps/web/src/store/useAuthStore.ts from nathycodes/presentercoach: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/spaces/nathycodes/presentercoach/resolve/main/apps/web/src/store/useAuthStore.ts
- Command line
-
hf download hf://spaces/nathycodes/presentercoach/apps/web/src/store/useAuthStore.ts
-
curl -L -o useAuthStore.ts https://huggingface.co/spaces/nathycodes/presentercoach/resolve/main/apps/web/src/store/useAuthStore.ts
2.53 kB
| import { create } from "zustand"; | |
| import { | |
| authApi, | |
| clearToken, | |
| ensureAccessToken, | |
| hasValidToken, | |
| setToken, | |
| } from "@/lib/api"; | |
| type AuthState = { | |
| userEmail: string | null; | |
| loading: boolean; | |
| bootstrapped: boolean; | |
| bootstrap: () => Promise<boolean>; | |
| login: (email: string, password: string) => Promise<void>; | |
| signup: (email: string, password: string) => Promise<void>; | |
| setup: (email: string, password: string) => Promise<void>; | |
| logout: () => Promise<void>; | |
| }; | |
| export const useAuthStore = create<AuthState>((set, get) => ({ | |
| userEmail: null, | |
| loading: false, | |
| bootstrapped: false, | |
| bootstrap: async () => { | |
| const state = get(); | |
| if (state.bootstrapped && state.userEmail && hasValidToken()) { | |
| return true; | |
| } | |
| const token = await ensureAccessToken(); | |
| if (!token) { | |
| set({ userEmail: null, bootstrapped: true }); | |
| return false; | |
| } | |
| try { | |
| const user = await authApi.me(); | |
| set({ userEmail: user.email, bootstrapped: true }); | |
| return true; | |
| } catch { | |
| clearToken(); | |
| set({ userEmail: null, bootstrapped: true }); | |
| return false; | |
| } | |
| }, | |
| login: async (email, password) => { | |
| set({ loading: true }); | |
| try { | |
| const token = await authApi.login(email, password); | |
| setToken(token.access_token, token.expires_in, token.refresh_token); | |
| set({ | |
| userEmail: token.user?.email || email, | |
| loading: false, | |
| bootstrapped: true, | |
| }); | |
| } catch (error) { | |
| set({ loading: false }); | |
| throw error; | |
| } | |
| }, | |
| signup: async (email, password) => { | |
| set({ loading: true }); | |
| try { | |
| const token = await authApi.signup(email, password); | |
| setToken(token.access_token, token.expires_in, token.refresh_token); | |
| set({ | |
| userEmail: token.user?.email || email, | |
| loading: false, | |
| bootstrapped: true, | |
| }); | |
| } catch (error) { | |
| set({ loading: false }); | |
| throw error; | |
| } | |
| }, | |
| setup: async (email, password) => { | |
| set({ loading: true }); | |
| try { | |
| const token = await authApi.setup(email, password); | |
| setToken(token.access_token, token.expires_in, token.refresh_token); | |
| set({ | |
| userEmail: token.user?.email || email, | |
| loading: false, | |
| bootstrapped: true, | |
| }); | |
| } catch (error) { | |
| set({ loading: false }); | |
| throw error; | |
| } | |
| }, | |
| logout: async () => { | |
| await authApi.logout().catch(() => {}); | |
| clearToken(); | |
| set({ userEmail: null, bootstrapped: true }); | |
| }, | |
| })); | |