diff --git a/web/src/lib/auth.svelte.ts b/web/src/lib/auth.svelte.ts index 7bb38808d..115dd858e 100644 --- a/web/src/lib/auth.svelte.ts +++ b/web/src/lib/auth.svelte.ts @@ -72,7 +72,7 @@ export type AuthState = | { kind: "logged-out" } | { kind: "loading"; did: Did | null; profile: AuthProfile | null } | { kind: "authenticating" } - | { kind: "profile-loading"; agent: OAuthUserAgent; did: Did } + | { kind: "profile-loading"; agent: OAuthUserAgent; did: Did; profile: AuthProfile | null } | { kind: "authenticated"; agent: OAuthUserAgent; profile: AuthProfile } | { kind: "failed"; @@ -240,7 +240,7 @@ export const createAuth = ( const hydrateProfile = async (did: Did, nextAgent: OAuthUserAgent) => { const resolved = await resolveProfile(did, bobbinUrl); if (state.kind !== "profile-loading" || state.agent !== nextAgent) return; - const nextProfile = resolved ?? { did, handle: did }; + const nextProfile = resolved ?? state.profile ?? { did, handle: did }; state = { kind: "authenticated", agent: nextAgent, profile: nextProfile }; const meta = upsertAccount(loadAccounts(), { did, @@ -256,9 +256,16 @@ export const createAuth = ( const nextAgent = new OAuthUserAgent(session); const did = nextAgent.sub as Did; otherAgents.delete(did); - state = { kind: "profile-loading", agent: nextAgent, did }; + const previous = currentProfile(); const known = loadAccounts().find((account) => account.did === did); - persistActive(did, known?.handle ?? did); + const handle = known?.handle ?? (previous?.did === did ? previous.handle : null); + state = { + kind: "profile-loading", + agent: nextAgent, + did, + profile: handle ? { did, handle } : null + }; + persistActive(did, handle ?? did); void hydrateProfile(did, nextAgent); }; @@ -475,7 +482,7 @@ export const createAuth = ( get resolving() { return ( state.kind === "authenticating" || - state.kind === "profile-loading" || + (state.kind === "profile-loading" && state.profile === null) || (state.kind === "loading" && state.did === null) ); },