diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 5df03e58c..00477783a 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -2418,55 +2418,70 @@ export default class GoTrueClient { /** * {@see GoTrueMFAApi#verify} */ - private async _verify(params: MFAVerifyTOTPParams): Promise - private async _verify(params: MFAVerifyPhoneParams): Promise - private async _verify(params: MFAVerifyWebAuthnParams): Promise { + private async _verify(params: MFAVerifyParams): Promise { return this._acquireLock(-1, async () => { try { - return await this._useSession(async (result) => { + const result = await this._useSession(async (result) => { const { data: sessionData, error: sessionError } = result if (sessionError) { return { data: null, error: sessionError } } - let requestBody: Record - if ('code' in params) { + if ('code' in params && 'challengeId' in params && 'factorId' in params) { // This handles MFAVerifyTOTPParams and MFAVerifyPhoneParams - requestBody = { - code: params.code, - challenge_id: params.challengeId, - } - } else { - // This handles MFAVerifyWebAuthnParams - requestBody = { - challenge_id: params.challengeId, - } - if (params.useMultiStepVerify !== undefined) { - requestBody.use_multi_step_verify = params.useMultiStepVerify + const { data, error } = await _request( + this.fetch, + 'POST', + `${this.url}/factors/${params.factorId}/verify`, + { + body: { + code: params.code, + challenge_id: params.challengeId, + }, + headers: this.headers, + jwt: sessionData?.session?.access_token, + } + ) + if (error) { + return { data: null, error } } - } - const { data, error } = await _request( - this.fetch, - 'POST', - `${this.url}/factors/${params.factorId}/verify`, - { - body: requestBody, - headers: this.headers, - jwt: sessionData?.session?.access_token, + await this._saveSession({ + expires_at: Math.round(Date.now() / 1000) + data.expires_in, + ...data, + }) + await this._notifyAllSubscribers('MFA_CHALLENGE_VERIFIED', data) + return { data, error } + } else if ('factorType' in params && params.factorType === 'webauthn') { + // TODO: Replace the placeholder + const { data, error } = await _request( + this.fetch, + 'POST', + `${this.url}/factors/verify`, + { + body: { + use_multi_step: params.useMultiStep, + factorType: params.factorType, + }, + headers: this.headers, + jwt: sessionData?.session?.access_token, + } + ) + if (error) { + return { data: null, error } } - ) - if (error) { - return { data: null, error } + await this._saveSession({ + expires_at: Math.round(Date.now() / 1000) + data.expires_in, + ...data, + }) + await this._notifyAllSubscribers('MFA_CHALLENGE_VERIFIED', data) + return { data, error } } - - await this._saveSession({ - expires_at: Math.round(Date.now() / 1000) + data.expires_in, - ...data, - }) - await this._notifyAllSubscribers('MFA_CHALLENGE_VERIFIED', data) - - return { data, error } + // TODO: fix this hack + // If we reach here, it means none of the conditions were met + return { data: null, error: new Error('Invalid MFA parameters') } }) + // TODO: Fix thsi hack + return result } catch (error) { if (isAuthError(error)) { return { data: null, error } diff --git a/src/lib/types.ts b/src/lib/types.ts index 28184ed17..4b1e9a775 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -826,11 +826,8 @@ export type MFAEnrollWebAuthnParams = { /** Human readable name assigned to the factor. */ friendlyName?: string - /** WebAuthn specific parameters*/ - webAuthn?: Object - /** Have the Auth client library handle the browser-authenticator interaction for you */ - useMultiStepEnroll: boolean + useMultiStep?: boolean } export type MFAVerifyTOTPParams = { @@ -848,14 +845,11 @@ export type MFAVerifyTOTPParams = { export type MFAVerifyPhoneParams = MFAVerifyTOTPParams export type MFAVerifyWebAuthnParams = { - /** ID of the factor being verified. Returned in enroll(). */ - factorId: string - - /** ID of the challenge being verified. Returned in challenge(). */ - challengeId: string + /** The type of factor being enrolled. */ + factorType: 'webauthn' /** Have the Auth client library handle the browser-authenticator interaction for you */ - useMultiStepVerify?: boolean + useMultiStep?: boolean } export type MFAEnrollParams = MFAEnrollTOTPParams | MFAEnrollPhoneParams | MFAEnrollWebAuthnParams