Align on what happens when expired

This commit is contained in:
Eric Bailey
2024-04-03 18:05:53 -05:00
parent 0ba76deb0c
commit de370bd2cc
+27 -20
View File
@@ -191,7 +191,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[setState], [setState],
) )
const upsertAccount = React.useCallback( const setCurrentAccount = React.useCallback(
(account: SessionAccount, expired = false) => { (account: SessionAccount, expired = false) => {
setStateAndPersist(s => { setStateAndPersist(s => {
return { return {
@@ -207,6 +207,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
const clearCurrentAccount = React.useCallback(() => { const clearCurrentAccount = React.useCallback(() => {
logger.warn(`session: clear current account`) logger.warn(`session: clear current account`)
__globalAgent = PUBLIC_BSKY_AGENT __globalAgent = PUBLIC_BSKY_AGENT
BskyAgent.configure({appLabelers: [BSKY_LABELER_DID]})
setStateAndPersist(s => ({ setStateAndPersist(s => ({
...s, ...s,
currentAccount: undefined, currentAccount: undefined,
@@ -275,22 +276,22 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
account, account,
({expired, refreshedAccount}) => { ({expired, refreshedAccount}) => {
if (expired) { if (expired) {
__globalAgent = PUBLIC_BSKY_AGENT clearCurrentAccount()
} }
upsertAccount(refreshedAccount, expired) setCurrentAccount(refreshedAccount, expired)
}, },
{networkErrorCallback: clearCurrentAccount}, {networkErrorCallback: clearCurrentAccount},
), ),
) )
__globalAgent = agent __globalAgent = agent
upsertAccount(account) setCurrentAccount(account)
logger.debug(`session: created account`, {}, logger.DebugContext.session) logger.debug(`session: created account`, {}, logger.DebugContext.session)
track('Create Account') track('Create Account')
logEvent('account:create:success', {}) logEvent('account:create:success', {})
}, },
[upsertAccount, clearCurrentAccount], [setCurrentAccount, clearCurrentAccount],
) )
const login = React.useCallback<ApiContext['login']>( const login = React.useCallback<ApiContext['login']>(
@@ -323,23 +324,23 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
account, account,
({expired, refreshedAccount}) => { ({expired, refreshedAccount}) => {
if (expired) { if (expired) {
__globalAgent = PUBLIC_BSKY_AGENT clearCurrentAccount()
} }
upsertAccount(refreshedAccount, expired) setCurrentAccount(refreshedAccount, expired)
}, },
{networkErrorCallback: clearCurrentAccount}, {networkErrorCallback: clearCurrentAccount},
), ),
) )
__globalAgent = agent __globalAgent = agent
upsertAccount(account) setCurrentAccount(account)
logger.debug(`session: logged in`, {}, logger.DebugContext.session) logger.debug(`session: logged in`, {}, logger.DebugContext.session)
track('Sign In', {resumedSession: false}) track('Sign In', {resumedSession: false})
logEvent('account:loggedIn', {logContext, withPassword: true}) logEvent('account:loggedIn', {logContext, withPassword: true})
}, },
[upsertAccount, clearCurrentAccount], [setCurrentAccount, clearCurrentAccount],
) )
const logout = React.useCallback<ApiContext['logout']>( const logout = React.useCallback<ApiContext['logout']>(
@@ -369,15 +370,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
service: account.service, service: account.service,
persistSession: createPersistSessionHandler( persistSession: createPersistSessionHandler(
account, account,
async ({expired, refreshedAccount}) => { ({expired, refreshedAccount}) => {
if (expired) { if (expired) {
__globalAgent = PUBLIC_BSKY_AGENT clearCurrentAccount()
} else { } else {
__globalAgent = agent __globalAgent = agent
await configureModeration(agent, account) setCurrentAccount(refreshedAccount, expired)
} }
upsertAccount(refreshedAccount, expired)
}, },
{networkErrorCallback: clearCurrentAccount}, {networkErrorCallback: clearCurrentAccount},
), ),
@@ -407,6 +406,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logger.error(`session: could not decode jwt`) logger.error(`session: could not decode jwt`)
} }
// optimistic, we'll update this if we can't reuse or resume the session
await configureModeration(agent, account)
if (canReusePrevSession) { if (canReusePrevSession) {
logger.debug( logger.debug(
`session: attempting to reuse previous session`, `session: attempting to reuse previous session`,
@@ -415,18 +417,23 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
) )
agent.session = prevSession agent.session = prevSession
__globalAgent = agent __globalAgent = agent
await configureModeration(agent, account) setCurrentAccount(account)
upsertAccount(account)
} else { } else {
logger.debug( logger.debug(
`session: attempting to resume using previous session`, `session: attempting to resumeSession using previous session`,
{}, {},
logger.DebugContext.session, logger.DebugContext.session,
) )
await networkRetry(1, () => agent.resumeSession(prevSession)) try {
// will call `persistSession` on `BskyAgent` instance above if success
await networkRetry(1, () => agent.resumeSession(prevSession))
} catch (e) {
logger.error(`session: resumeSession failed`, {message: e})
clearCurrentAccount()
}
} }
}, },
[upsertAccount, clearCurrentAccount], [setCurrentAccount, clearCurrentAccount],
) )
const resumeSession = React.useCallback<ApiContext['resumeSession']>( const resumeSession = React.useCallback<ApiContext['resumeSession']>(
@@ -544,7 +551,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
/* /*
* Use updated session in this tab's agent. Do not call * Use updated session in this tab's agent. Do not call
* upsertAccount, since that will only persist the session that's * setCurrentAccount, since that will only persist the session that's
* already persisted, and we'll get a loop between tabs. * already persisted, and we'll get a loop between tabs.
*/ */
// @ts-ignore we checked for `refreshJwt` above // @ts-ignore we checked for `refreshJwt` above