fix: resolve login persistence bug and aggressive logout on network blips
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m58s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m58s
- Updated Login.tsx to automatically redirect users to the dashboard if a valid token is already present in localStorage. - Refactored getUserById to properly throw server/network errors instead of silently returning undefined. - Updated AuthGuard in App.tsx to gracefully handle network errors without destroying the user's valid localStorage tokens.
This commit is contained in:
@@ -280,16 +280,20 @@ export const getUserById = async (id: string): Promise<User | undefined> => {
|
||||
const response = await fetch(`${API_URL}/users/${id}`, {
|
||||
headers: getHeaders()
|
||||
});
|
||||
if (!response.ok) return undefined;
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401 || response.status === 403 || response.status === 404) {
|
||||
return undefined; // Invalid user or token
|
||||
}
|
||||
throw new Error(`Server error: ${response.status}`);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get("content-type");
|
||||
if (contentType && contentType.indexOf("application/json") !== -1) {
|
||||
return await response.json();
|
||||
}
|
||||
return undefined;
|
||||
} catch (error) {
|
||||
console.error("API Error (getUserById):", error);
|
||||
return undefined;
|
||||
throw error; // Rethrow so AuthGuard catches it and doesn't wipe tokens
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user