Skip to content

Commit b3f9fcd

Browse files
committed
Avoid concurrent currentSession calls.
1 parent dc34a3c commit b3f9fcd

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/solid-auth-client.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export type loginOptions = {
2020
}
2121

2222
export default class SolidAuthClient extends EventEmitter {
23+
_pendingSession: ?Promise<?Session>
24+
2325
fetch(input: RequestInfo, options?: RequestOptions): Promise<Response> {
2426
return authnFetch(defaultStorage(), globalFetch, input, options)
2527
}
@@ -50,17 +52,30 @@ export default class SolidAuthClient extends EventEmitter {
5052
async currentSession(
5153
storage: AsyncStorage = defaultStorage()
5254
): Promise<?Session> {
55+
// Try to obtain a stored session
5356
let session = await getSession(storage)
57+
58+
// Try to create a new session
5459
if (!session) {
60+
// Return if session creation is already pending
61+
if (this._pendingSession) {
62+
return this._pendingSession
63+
}
64+
65+
// Create a new OIDC session
5566
try {
56-
session = await WebIdOidc.currentSession(storage)
67+
this._pendingSession = WebIdOidc.currentSession(storage)
68+
session = await this._pendingSession
5769
} catch (err) {
5870
console.error(err)
5971
}
72+
delete this._pendingSession
73+
74+
// Save the session and emit session events
6075
if (session) {
76+
await saveSession(storage)(session)
6177
this.emit('login', session)
6278
this.emit('session', session)
63-
await saveSession(storage)(session)
6479
}
6580
}
6681
return session

0 commit comments

Comments
 (0)