perf: use cached user data for faster login redirect

This commit is contained in:
soufiane 2025-12-03 21:20:43 +01:00
parent 29b4e27baa
commit 76e49559e0

View File

@ -46,6 +46,30 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ 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);