Skip to content

Commit 864fd6b

Browse files
authored
Merge pull request #76 from blacksky-algorithms/fix/oauth-session-restore
Fix OAuth session restore hangs and false Logged out status
2 parents 7e7ac58 + 85c8e06 commit 864fd6b

3 files changed

Lines changed: 59 additions & 10 deletions

File tree

src/components/AccountList.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ function AccountItem({
122122
onSelect(account)
123123
}, [account, onSelect])
124124

125-
const isLoggedOut = !account.refreshJwt || isJwtExpired(account.refreshJwt)
125+
const isLoggedOut = account.isOauthSession
126+
? false // OAuth sessions are managed by the OAuth client, not refreshJwt
127+
: !account.refreshJwt || isJwtExpired(account.refreshJwt)
126128

127129
return (
128130
<Button

src/screens/Login/ChooseAccountForm.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ export const ChooseAccountForm = ({
4646
}
4747
try {
4848
setPendingDid(account.did)
49-
await resumeSession(account, true)
49+
await Promise.race([
50+
resumeSession(account, true),
51+
new Promise<never>((_, reject) =>
52+
setTimeout(
53+
() => reject(new Error('Session resume timed out')),
54+
15_000,
55+
),
56+
),
57+
])
5058
ax.metric('account:loggedIn', {
5159
logContext: 'ChooseAccountForm',
5260
withPassword: false,
@@ -56,6 +64,7 @@ export const ChooseAccountForm = ({
5664
logger.error('choose account: initSession failed', {
5765
message: e.message,
5866
})
67+
Toast.show(_(msg`Sign in failed. Please try again.`))
5968
// Move to login form.
6069
onSelectAccount(account)
6170
} finally {

src/state/session/oauth-agent.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,28 @@ export async function oauthCreateAgent(session: OAuthSession) {
2020
return agent.prepare(account, gates, moderation)
2121
}
2222

23+
const OAUTH_RESTORE_TIMEOUT_MS = 10_000
24+
2325
export async function oauthResumeSession(account: SessionAccount) {
2426
const client = getWebOAuthClient()
25-
const session = await client.restore(account.did)
27+
let session: OAuthSession
28+
try {
29+
session = await Promise.race([
30+
client.restore(account.did),
31+
new Promise<never>((_, reject) =>
32+
setTimeout(
33+
() => reject(new Error('OAuth session restore timed out')),
34+
OAUTH_RESTORE_TIMEOUT_MS,
35+
),
36+
),
37+
])
38+
} catch (e) {
39+
logger.error('oauthResumeSession: restore failed', {
40+
did: account.did,
41+
error: e instanceof Error ? e.message : String(e),
42+
})
43+
throw e
44+
}
2645
return await oauthCreateAgent(session)
2746
}
2847

@@ -43,13 +62,36 @@ export async function oauthAgentAndSessionToSessionAccount(
4362
): Promise<SessionAccount | undefined> {
4463
let data: OutputSchema
4564
try {
46-
const res = await agent.com.atproto.server.getSession()
65+
const res = await Promise.race([
66+
agent.com.atproto.server.getSession(),
67+
new Promise<never>((_, reject) =>
68+
setTimeout(
69+
() => reject(new Error('getSession timed out')),
70+
OAUTH_RESTORE_TIMEOUT_MS,
71+
),
72+
),
73+
])
4774
data = res.data
4875
} catch (e: any) {
49-
logger.error(e)
76+
logger.error('oauthAgentAndSessionToSessionAccount: getSession failed', e)
77+
return undefined
78+
}
79+
let aud: string
80+
try {
81+
const tokenInfo = await Promise.race([
82+
session.getTokenInfo(false),
83+
new Promise<never>((_, reject) =>
84+
setTimeout(
85+
() => reject(new Error('getTokenInfo timed out')),
86+
OAUTH_RESTORE_TIMEOUT_MS,
87+
),
88+
),
89+
])
90+
aud = tokenInfo.aud
91+
} catch (e: any) {
92+
logger.error('oauthAgentAndSessionToSessionAccount: getTokenInfo failed', e)
5093
return undefined
5194
}
52-
const {aud} = await session.getTokenInfo(false)
5395
return {
5496
service: session.serverMetadata.issuer,
5597
did: session.did,
@@ -69,12 +111,8 @@ export class OauthBskyAppAgent extends Agent {
69111
session?: AtpSessionData
70112
dispatchUrl?: string
71113

72-
#oauthSession: OAuthSession
73-
#account?: SessionAccount
74-
75114
constructor(session: OAuthSession) {
76115
super(session)
77-
this.#oauthSession = session
78116
}
79117

80118
async prepare(

0 commit comments

Comments
 (0)