diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts index eb1685a0ae..0dc60d9323 100644 --- a/src/lib/hooks/useAccountSwitcher.ts +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -1,11 +1,12 @@ import {useCallback} from 'react' -import {isWeb} from '#/platform/detection' import {useAnalytics} from '#/lib/analytics/analytics' -import {useSessionApi, SessionAccount} from '#/state/session' -import * as Toast from '#/view/com/util/Toast' -import {useCloseAllActiveElements} from '#/state/util' +import {logger} from '#/logger' +import {isWeb} from '#/platform/detection' +import {SessionAccount, useSessionApi} from '#/state/session' import {useLoggedOutViewControls} from '#/state/shell/logged-out' +import {useCloseAllActiveElements} from '#/state/util' +import * as Toast from '#/view/com/util/Toast' import {LogEvents} from '../statsig/statsig' export function useAccountSwitcher() { @@ -44,9 +45,14 @@ export function useAccountSwitcher() { 'circle-exclamation', ) } - } catch (e) { - Toast.show('Sorry! We need you to enter your password.') + } catch (e: any) { + logger.error(`switch account: selectAccount failed`, { + message: e.message, + }) clearCurrentAccount() // back user out to login + setTimeout(() => { + Toast.show('Sorry! We need you to enter your password.') + }, 100) } }, [ diff --git a/src/screens/Login/ChooseAccountForm.tsx b/src/screens/Login/ChooseAccountForm.tsx index 134411903d..d36d109773 100644 --- a/src/screens/Login/ChooseAccountForm.tsx +++ b/src/screens/Login/ChooseAccountForm.tsx @@ -5,6 +5,7 @@ import {useLingui} from '@lingui/react' import {useAnalytics} from '#/lib/analytics/analytics' import {logEvent} from '#/lib/statsig/statsig' +import {logger} from '#/logger' import {SessionAccount, useSession, useSessionApi} from '#/state/session' import {useLoggedOutViewControls} from '#/state/shell/logged-out' import * as Toast from '#/view/com/util/Toast' @@ -38,15 +39,22 @@ export const ChooseAccountForm = ({ setShowLoggedOut(false) Toast.show(_(msg`Already signed in as @${account.handle}`)) } else { - await initSession(account) - logEvent('account:loggedIn', { - logContext: 'ChooseAccountForm', - withPassword: false, - }) - track('Sign In', {resumedSession: true}) - setTimeout(() => { - Toast.show(_(msg`Signed in as @${account.handle}`)) - }, 100) + try { + await initSession(account) + logEvent('account:loggedIn', { + logContext: 'ChooseAccountForm', + withPassword: false, + }) + track('Sign In', {resumedSession: true}) + setTimeout(() => { + Toast.show(_(msg`Signed in as @${account.handle}`)) + }, 100) + } catch (e: any) { + logger.error('choose account: initSession failed', { + message: e.message, + }) + onSelectAccount(account) + } } } else { onSelectAccount(account) diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index 9eb84a3b38..3f57d9c113 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -213,6 +213,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { console.log('PERSIST', window.__id, { event, refreshJwt: session?.refreshJwt?.slice(-10), + agent: agent.session?.refreshJwt.slice(-10), }) const expired = event === 'expired' || event === 'create-failed' @@ -226,7 +227,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) { return } - agent.session = session const refreshedAccount = agentToSessionAccount(agent) if (!refreshedAccount) { @@ -414,19 +414,12 @@ export function Provider({children}: React.PropsWithChildren<{}>) { {}, logger.DebugContext.session, ) - try { - // will call `persistSession` on `BskyAgent` instance above if success - await networkRetry(1, () => agent.resumeSession(prevSession)) - setCurrentAgent(agent) - } catch (e) { - // this can fail on bad connections as well, so `clearCurrentAccount` - // bumps them out to login, but doesn't rug tokens - logger.error(`session: resumeSession failed`, {message: e}) - clearCurrentAccount() - } + // will call `persistSession` on `BskyAgent` instance above if success + await networkRetry(1, () => agent.resumeSession(prevSession)) + setCurrentAgent(agent) } }, - [upsertAndPersistAccount, clearCurrentAccount, persistSession], + [upsertAndPersistAccount, persistSession], ) const resumeSession = React.useCallback(