Skip to content

Commit

Permalink
fix: refactor _challenge structure
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Oct 2, 2024
1 parent f445ba5 commit 4c6f7ba
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2622,30 +2622,6 @@ export default class GoTrueClient {
* {@see GoTrueMFAApi#challenge}
*/
private async _challenge(params: MFAChallengeParams): Promise<AuthMFAChallengeResponse> {
let body: { channel?: 'sms' | 'whatsapp' } | { webAuthn?: { rpId: string } } | {} = {}
if ('webAuthn' in params && params.webAuthn?.rpId) {
body = {
web_authn: { rp_id: params.webAuthn.rpId, rp_origins: params.webAuthn.rpOrigins },
}
return await this._useSession(async (result) => {
const { data: sessionData, error: sessionError } = result
if (sessionError) {
return { data: null, error: sessionError }
}

return await _request(
this.fetch,
'POST',
`${this.url}/factors/${params.factorId}/challenge`,
{
body,
headers: this.headers,
jwt: sessionData?.session?.access_token,
}
)
})
}

return this._acquireLock(-1, async () => {
try {
return await this._useSession(async (result) => {
Expand All @@ -2654,9 +2630,18 @@ export default class GoTrueClient {
return { data: null, error: sessionError }
}

if ('channel' in params) {
let body: Record<string, any> = {}
if ('webAuthn' in params && params.webAuthn?.rpId) {
body = {
web_authn: {
rp_id: params.webAuthn.rpId,
rp_origins: params.webAuthn.rpOrigins,
},
}
} else if ('channel' in params) {
body = { channel: params.channel }
}

return await _request(
this.fetch,
'POST',
Expand Down

0 comments on commit 4c6f7ba

Please sign in to comment.