UNPKG

@oxyhq/services

Version:

OxyHQ Expo/React Native SDK — UI components, screens, and native features

64 lines (62 loc) 1.67 kB
"use strict"; import { create } from 'zustand'; import { createDebugLogger } from '@oxyhq/core'; const debug = createDebugLogger('AuthStore'); export const useAuthStore = create((set, get) => ({ user: null, isAuthenticated: false, isLoading: false, error: null, lastUserFetch: null, loginSuccess: user => set({ isLoading: false, isAuthenticated: true, user, lastUserFetch: Date.now() }), loginFailure: error => set({ isLoading: false, error }), logout: () => set({ user: null, isAuthenticated: false, lastUserFetch: null }), setUser: user => set({ user, lastUserFetch: Date.now() }), fetchUser: async (oxyServices, forceRefresh = false) => { const state = get(); const now = Date.now(); const cacheAge = state.lastUserFetch ? now - state.lastUserFetch : Number.POSITIVE_INFINITY; const cacheValid = cacheAge < 5 * 60 * 1000; // 5 minutes cache // Use cached data if available and not forcing refresh if (!forceRefresh && state.user && cacheValid) { debug.log('Using cached user data (age:', cacheAge, 'ms)'); return; } set({ isLoading: true, error: null }); try { const user = await oxyServices.getCurrentUser(); set({ user, isLoading: false, isAuthenticated: true, lastUserFetch: now }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Failed to fetch user'; debug.error('Error fetching user:', error); set({ error: errorMessage, isLoading: false }); } } })); //# sourceMappingURL=authStore.js.map