Optimize AuthContext profile and roles fetching - reduce timeout and parallelize

cgen-1e2d2bf6be8c4afd90dbd95755cf75fc
This commit is contained in:
Builder.io 2025-11-05 06:36:45 +00:00
parent 6171495caa
commit acfd6aeec6

View file

@ -298,54 +298,52 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
userId: string, userId: string,
): Promise<AethexUserProfile | null> => { ): Promise<AethexUserProfile | null> => {
try { try {
// Add 5-second timeout to prevent hanging // Add 3-second timeout for profile fetch
const profilePromise = aethexUserService.getCurrentUser(); const profilePromise = aethexUserService.getCurrentUser();
const timeoutPromise = new Promise<never>((_, reject) => const profileTimeoutPromise = new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error("Profile fetch timeout")), 5000), setTimeout(() => reject(new Error("Profile fetch timeout")), 3000),
); );
const userProfile = await Promise.race([profilePromise, timeoutPromise]); const userProfile = await Promise.race([profilePromise, profileTimeoutPromise]);
setProfile(userProfile); setProfile(userProfile);
try { // Fetch roles in parallel (non-blocking) - don't await here
const rolesPromise = aethexRoleService.getUserRoles(userId); const rolesPromise = aethexRoleService
const rolesTimeoutPromise = new Promise<never>((_, reject) => .getUserRoles(userId)
setTimeout(() => reject(new Error("Roles fetch timeout")), 5000), .then((r) => {
); // Auto-seed owner roles if logging in as site owner
const normalizedEmail = userProfile?.email?.toLowerCase();
let r = await Promise.race([rolesPromise, rolesTimeoutPromise]); if (normalizedEmail === "mrpiglr@gmail.com" && !r.includes("owner")) {
const seeded = Array.from(
// Auto-seed owner roles if logging in as site owner new Set(["owner", "admin", "founder", ...r]),
const normalizedEmail = userProfile?.email?.toLowerCase(); );
if (normalizedEmail === "mrpiglr@gmail.com" && !r.includes("owner")) { return aethexRoleService
const seeded = Array.from( .setUserRoles(userId, seeded)
new Set(["owner", "admin", "founder", ...r]), .then(() => seeded)
); .catch(() => r);
try {
await aethexRoleService.setUserRoles(userId, seeded);
r = seeded;
} catch {
// Failed to set roles, continue with current roles
} }
} if (
if ( normalizedEmail &&
normalizedEmail && /@aethex\.dev$/i.test(normalizedEmail) &&
/@aethex\.dev$/i.test(normalizedEmail) && !r.includes("staff")
!r.includes("staff") ) {
) { const seeded = Array.from(new Set(["staff", ...r]));
const seeded = Array.from(new Set(["staff", ...r])); return aethexRoleService
try { .setUserRoles(userId, seeded)
await aethexRoleService.setUserRoles(userId, seeded); .then(() => seeded)
r = seeded; .catch(() => r);
} catch {
// Failed to set roles, continue with current roles
} }
} return r;
setRoles(r); })
} catch (error) { .then((r) => {
console.warn("Error fetching roles:", error); setRoles(r);
setRoles([]); })
} .catch((error) => {
console.warn("Error fetching roles:", error);
setRoles([]);
});
// Don't wait for rolesPromise - continue immediately
setLoading(false); setLoading(false);
return userProfile; return userProfile;
} catch (error) { } catch (error) {