Skip to content

Commit 9c52eef

Browse files
authored
fix(auth): ensure token exists before persisting connection (#2984)
## Problem Connections are created before the user finishes the auth flow. This can be confusing because the connections aren't marked as 'pending'. ## Solution Don't persist the connection until we get a token.
1 parent edccb18 commit 9c52eef

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/credentials/auth.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,21 @@ export class Auth implements AuthService, ConnectionManager {
380380

381381
// XXX: `id` should be based off the resolved `idToken`, _not_ the source profile
382382
const id = getSsoProfileKey(profile)
383-
const storedProfile = await this.store.addProfile(id, profile)
384-
const conn = this.getSsoConnection(id, storedProfile)
383+
const tokenProvider = this.getTokenProvider(id, {
384+
...profile,
385+
metadata: { connectionState: 'unauthenticated' },
386+
})
385387

386388
try {
387-
await conn.getToken()
389+
;(await tokenProvider.getToken()) ?? (await tokenProvider.createToken())
390+
const storedProfile = await this.store.addProfile(id, profile)
391+
await this.updateConnectionState(id, 'valid')
392+
393+
return this.getSsoConnection(id, storedProfile)
388394
} catch (err) {
389395
await this.store.deleteProfile(id)
390396
throw err
391397
}
392-
393-
return this.getSsoConnection(id, storedProfile)
394398
}
395399

396400
public async deleteConnection(connection: Pick<Connection, 'id'>): Promise<void> {

0 commit comments

Comments
 (0)