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 Oct 9, 2024
1 parent a7db0ee commit 0b317d4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 40 deletions.
85 changes: 51 additions & 34 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2409,50 +2409,67 @@ export default class GoTrueClient {
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
10 changes: 4 additions & 6 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ export type GenerateLinkType =
| 'email_change_current'
| 'email_change_new'


export type MFAVerifyTOTPParams = {
/** ID of the factor being verified. Returned in enroll(). */
factorId: string
Expand All @@ -823,14 +824,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 0b317d4

Please sign in to comment.