Skip to content

Commit

Permalink
fix: temp workaround on types
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Sep 19, 2024
1 parent 9b74b4f commit 41c22fb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
89 changes: 52 additions & 37 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2418,55 +2418,70 @@ export default class GoTrueClient {
/**
* {@see GoTrueMFAApi#verify}
*/
private async _verify(params: MFAVerifyTOTPParams): Promise<AuthMFAVerifyResponse>
private async _verify(params: MFAVerifyPhoneParams): Promise<AuthMFAVerifyResponse>
private async _verify(params: MFAVerifyWebAuthnParams): Promise<AuthMFAVerifyResponse> {
private async _verify(params: MFAVerifyParams): Promise<AuthMFAVerifyResponse> {
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<string, unknown>

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 }
Expand Down
14 changes: 4 additions & 10 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand Down

0 comments on commit 41c22fb

Please sign in to comment.