diff --git a/src/state/session/agent.ts b/src/state/session/agent.ts index e9801d62cb..7c4510b109 100644 --- a/src/state/session/agent.ts +++ b/src/state/session/agent.ts @@ -33,7 +33,18 @@ export async function createAgentAndResume( event: AtpSessionEvent, ) => void, ) { - const agent = new BskyAgent({service: storedAccount.service}) + const agent = new BskyAgent({ + service: storedAccount.service, + persistSession: event => { + const {session} = agent + if (!session?.did) return // @TODO SESSION - ? + + onSessionChange(agent, session.did, event) + if (event !== 'create' && event !== 'update') { + addSessionErrorLog(session.did, event) + } + }, + }) if (storedAccount.pdsUrl) { agent.sessionManager.pdsUrl = new URL(storedAccount.pdsUrl) } @@ -43,10 +54,9 @@ export async function createAgentAndResume( if (isSessionExpired(storedAccount)) { await networkRetry(1, () => agent.resumeSession(prevSession)) } else { - agent.session = prevSession if (!storedAccount.signupQueued) { - // Intentionally not awaited to unblock the UI: - networkRetry(3, () => agent.resumeSession(prevSession)).catch( + // @TODO SESSION - We were not awaiting this before. Why? And why is it now broken if we do not await? + await networkRetry(3, () => agent.resumeSession(prevSession)).catch( (e: any) => { logger.error(`networkRetry failed to resume session`, { status: e?.status || 'unknown', @@ -60,7 +70,7 @@ export async function createAgentAndResume( } } - return prepareAgent(agent, gates, moderation, onSessionChange) + return prepareAgent(agent, gates, moderation) } export async function createAgentAndLogin( @@ -81,41 +91,45 @@ export async function createAgentAndLogin( event: AtpSessionEvent, ) => void, ) { - const agent = new BskyAgent({service}) + const agent = new BskyAgent({ + service, + persistSession: event => { + const {session} = agent + if (!session?.did) return // @TODO SESSION - ? + + onSessionChange(agent, session.did, event) + if (event !== 'create' && event !== 'update') { + addSessionErrorLog(session.did, event) + } + }, + }) await agent.login({identifier, password, authFactorToken}) const account = agentToSessionAccountOrThrow(agent) const gates = tryFetchGates(account.did, 'prefer-fresh-gates') const moderation = configureModerationForAccount(agent, account) - return prepareAgent(agent, moderation, gates, onSessionChange) + return prepareAgent(agent, moderation, gates) } -export async function createAgentAndCreateAccount( - { - service, - email, - password, - handle, - birthDate, - inviteCode, - verificationPhone, - verificationCode, - }: { - service: string - email: string - password: string - handle: string - birthDate: Date - inviteCode?: string - verificationPhone?: string - verificationCode?: string - }, - onSessionChange: ( - agent: BskyAgent, - did: string, - event: AtpSessionEvent, - ) => void, -) { +export async function createAgentAndCreateAccount({ + service, + email, + password, + handle, + birthDate, + inviteCode, + verificationPhone, + verificationCode, +}: { + service: string + email: string + password: string + handle: string + birthDate: Date + inviteCode?: string + verificationPhone?: string + verificationCode?: string +}) { const agent = new BskyAgent({service}) await agent.createAccount({ email, @@ -174,7 +188,7 @@ export async function createAgentAndCreateAccount( logger.error(e, {context: `session: failed snoozeEmailConfirmationPrompt`}) } - return prepareAgent(agent, gates, moderation, onSessionChange) + return prepareAgent(agent, gates, moderation) } async function prepareAgent( @@ -182,23 +196,12 @@ async function prepareAgent( // Not awaited in the calling code so we can delay blocking on them. gates: Promise, moderation: Promise, - onSessionChange: ( - agent: BskyAgent, - did: string, - event: AtpSessionEvent, - ) => void, ) { // There's nothing else left to do, so block on them here. await Promise.all([gates, moderation]) // Now the agent is ready. const account = agentToSessionAccountOrThrow(agent) - agent.setPersistSessionHandler(event => { - onSessionChange(agent, account.did, event) - if (event !== 'create' && event !== 'update') { - addSessionErrorLog(account.did, event) - } - }) return {agent, account} } diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index 3aac19025d..6a80dd563f 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -14,7 +14,6 @@ import { createAgentAndCreateAccount, createAgentAndLogin, createAgentAndResume, - sessionAccountToSession, } from './agent' import {getInitialState, reducer} from './reducer' @@ -70,10 +69,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const signal = cancelPendingTask() track('Try Create Account') logEvent('account:create:begin', {}) - const {agent, account} = await createAgentAndCreateAccount( - params, - onAgentSessionChange, - ) + const {agent, account} = await createAgentAndCreateAccount(params) if (signal.aborted) { return @@ -87,7 +83,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { logEvent('account:create:success', {}) addSessionDebugLog({type: 'method:end', method: 'createAccount', account}) }, - [onAgentSessionChange, cancelPendingTask], + [cancelPendingTask], ) const login = React.useCallback( @@ -196,20 +192,12 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const syncedAccount = synced.accounts.find( a => a.did === synced.currentAccount?.did, ) - if (syncedAccount && syncedAccount.refreshJwt) { - if (syncedAccount.did !== state.currentAgentState.did) { - resumeSession(syncedAccount) - } else { - const agent = state.currentAgentState.agent as BskyAgent - const prevSession = agent.session - agent.session = sessionAccountToSession(syncedAccount) - addSessionDebugLog({ - type: 'agent:patch', - agent, - prevSession, - nextSession: agent.session, - }) - } + if ( + syncedAccount && + syncedAccount.refreshJwt && + syncedAccount.did !== state.currentAgentState.did + ) { + resumeSession(syncedAccount) } }) }, [state, resumeSession]) @@ -249,8 +237,8 @@ export function Provider({children}: React.PropsWithChildren<{}>) { addSessionDebugLog({type: 'agent:switch', prevAgent, nextAgent: agent}) // We never reuse agents so let's fully neutralize the previous one. // This ensures it won't try to consume any refresh tokens. - prevAgent.session = undefined - prevAgent.setPersistSessionHandler(undefined) + // prevAgent.session = undefined + // prevAgent.setPersistSessionHandler(undefined) } }, [agent])