fix bug where concurrent auth calls returned only one response

This commit is contained in:
codytseng 2025-05-09 10:36:20 +08:00 committed by fiatjaf_
parent 52079f6e75
commit c015b6e794
1 changed files with 6 additions and 5 deletions

View File

@ -224,7 +224,6 @@ export class AbstractRelay {
return
case 'AUTH': {
this.challenge = data[1] as string
this.authPromise = undefined
this._onauth?.(data[1] as string)
return
}
@ -243,10 +242,12 @@ export class AbstractRelay {
}
public async auth(signAuthEvent: (evt: EventTemplate) => Promise<VerifiedEvent>): Promise<string> {
if (!this.challenge) throw new Error("can't perform auth, no challenge was received")
const challenge = this.challenge
if (!challenge) throw new Error("can't perform auth, no challenge was received")
if (this.authPromise) return this.authPromise
const evt = await signAuthEvent(makeAuthEvent(this.url, this.challenge))
this.authPromise = new Promise<string>((resolve, reject) => {
this.authPromise = new Promise<string>(async (resolve, reject) => {
const evt = await signAuthEvent(makeAuthEvent(this.url, challenge))
const timeout = setTimeout(() => {
const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
if (ep) {
@ -255,8 +256,8 @@ export class AbstractRelay {
}
}, this.publishTimeout)
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
this.send('["AUTH",' + JSON.stringify(evt) + ']')
})
this.send('["AUTH",' + JSON.stringify(evt) + ']')
return this.authPromise
}