perf: use cached user data for faster login redirect
This commit is contained in:
parent
29b4e27baa
commit
76e49559e0
|
|
@ -46,6 +46,30 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
||||||
return;
|
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 {
|
try {
|
||||||
const userData = await authService.getCurrentUser();
|
const userData = await authService.getCurrentUser();
|
||||||
setUser(userData);
|
setUser(userData);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user