diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 425613e6..9f52a714 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -45,7 +45,6 @@ import { localStorageAdapter, memoryLocalStorageAdapter } from './lib/local-stor import { polyfillGlobalThis } from './lib/polyfills' import { version } from './lib/version' import { LockAcquireTimeoutError, navigatorLock } from './lib/locks' -import { startRegistration } from '@simplewebauthn/browser' import type { AuthChangeEvent, @@ -2809,23 +2808,24 @@ export default class GoTrueClient { private async _challengeAndVerify( params: MFAChallengeAndVerifyParams ): Promise { - if ('factorId' in params && 'code' in params) { - const { data: challengeResponse, error: challengeError } = await this._challenge({ - factorId: params.factorId, - }) - if (challengeError) { - return { data: null, error: challengeError } + if (!('factorId' in params && 'code' in params)) { + return { + data: null, + error: new AuthError('Invalid parameters', 400, 'invalid_parameters'), } - return await this._verify({ - factorId: params.factorId, - challengeId: challengeResponse.id, - code: params.code, - }) } - return { - data: null, - error: new AuthError('Unknown error', 500, 'unknown_error'), + const { factorId, code } = params + const { data: challengeResponse, error: challengeError } = await this._challenge({ + factorId: params.factorId, + }) + if (challengeError) { + return { data: null, error: challengeError } } + return await this._verify({ + factorId: params.factorId, + challengeId: challengeResponse.id, + code: params.code, + }) } /** @@ -2833,39 +2833,34 @@ export default class GoTrueClient { */ private async _listFactors(): Promise { // use #getUser instead of #_getUser as the former acquires a lock - try { - const { - data: { user }, - error: userError, - } = await this.getUser() - if (userError) { - return { data: null, error: userError } - } + const { + data: { user }, + error: userError, + } = await this.getUser() + if (userError) { + return { data: null, error: userError } + } - const factors = user?.factors || [] - const totp = factors.filter( - (factor) => factor.factor_type === 'totp' && factor.status === 'verified' - ) - const phone = factors.filter( - (factor) => factor.factor_type === 'phone' && factor.status === 'verified' - ) + const factors = user?.factors || [] + const totp = factors.filter( + (factor) => factor.factor_type === 'totp' && factor.status === 'verified' + ) + const phone = factors.filter( + (factor) => factor.factor_type === 'phone' && factor.status === 'verified' + ) - const webauthn = factors.filter( - (factor) => factor.factor_type === 'webauthn' && factor.status === 'verified' - ) + const webauthn = factors.filter( + (factor) => factor.factor_type === 'webauthn' && factor.status === 'verified' + ) - return { - data: { - all: factors, - totp, - phone, - webauthn, - }, - error: null, - } - } catch (error) { - console.error('Error in _listFactors:', error) - return { data: null, error: new AuthError('Failed to list factors') } + return { + data: { + all: factors, + totp, + phone, + webauthn, + }, + error: null, } }