From 09eb3b80c235582eddad8d324f28d185a262fdbd Mon Sep 17 00:00:00 2001 From: Xylight Date: Sat, 6 Jul 2024 11:17:08 -0700 Subject: [PATCH] fix: double load --- src/lib/auth.ts | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index f59a1934..6e0b54b3 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -92,6 +92,8 @@ export let profileData = writable( } ) +let fetchingUser = false + export let profile = derived, Profile>( profileData, (pd) => { @@ -99,23 +101,34 @@ export let profile = derived, Profile>( pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile() if (profile?.jwt) { - if (!profile.user) { + if (!profile.user && !fetchingUser) { site.set(undefined) - userFromJwt(profile.jwt, profile.instance).then((res) => { - if (!res?.user) - toast({ - content: 'Your login has expired. Re-login to fix this issue.', - type: 'warning', - }) + fetchingUser = true - site.set(res?.site) + notifications.set({ applications: 0, inbox: 0, reports: 0 }) - profile.user = res?.user - profile.avatar = res?.user.local_user_view.person.avatar + userFromJwt(profile.jwt, profile.instance) + .then((res) => { + if (!res?.user) + toast({ + content: 'Your login has expired. Re-login to fix this issue.', + type: 'warning', + }) - checkInbox() - }) + site.set(res?.site) + + profile.user = res?.user + profile.avatar = res?.user.local_user_view.person.avatar + + checkInbox() + + fetchingUser = false + }) + .catch((e) => { + fetchingUser = false + toast({ content: e, type: 'error' }) + }) } } else { site.set(undefined)