diff --git a/contexts/AuthContext.tsx b/contexts/AuthContext.tsx index d3d5e87..cc98ec3 100644 --- a/contexts/AuthContext.tsx +++ b/contexts/AuthContext.tsx @@ -46,6 +46,30 @@ export const AuthProvider: React.FC = ({ children }) => { return; } + // Try to load cached user first for faster initial render + const cachedUser = storage.get(STORAGE_KEYS.USER); + if (cachedUser) { + try { + const parsedUser = JSON.parse(cachedUser); + setUser(parsedUser); + setIsLoading(false); + + // Refresh user data in background (don't block UI) + authService.getCurrentUser() + .then(userData => { + setUser(userData); + storage.set(STORAGE_KEYS.USER, JSON.stringify(userData)); + }) + .catch(error => { + console.error('Error refreshing user:', error); + // If refresh fails, keep cached user + }); + return; + } catch (e) { + // Invalid cached data, continue to fetch + } + } + try { const userData = await authService.getCurrentUser(); setUser(userData);