From 76e49559e0b1a32fefe1f2ea4cccfec3c15f5ef0 Mon Sep 17 00:00:00 2001 From: soufiane Date: Wed, 3 Dec 2025 21:20:43 +0100 Subject: [PATCH] perf: use cached user data for faster login redirect --- contexts/AuthContext.tsx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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);