Skip to content

Commit

Permalink
fix: add autoenroll
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Sep 20, 2024
1 parent 98e6416 commit 094582b
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2371,8 +2371,10 @@ export default class GoTrueClient {
): Promise<AuthMFAEnrollPhoneResponse | AuthMFAEnrollErrorResponse>
private async _enroll(
params: MFAEnrollWebAuthnParams
): Promise<AuthMFAEnrollWebAuthnResponse | AuthMFAEnrollErrorResponse>
private async _enroll(params: MFAEnrollParams): Promise<AuthMFAEnrollResponse> {
): Promise<AuthMFAEnrollWebAuthnResponse | AuthMFAVerifyResponse | AuthMFAEnrollErrorResponse>
private async _enroll(
params: MFAEnrollParams
): Promise<AuthMFAEnrollResponse | AuthMFAVerifyResponse> {
try {
return await this._useSession(async (result) => {
const { data: sessionData, error: sessionError } = result
Expand Down Expand Up @@ -2403,6 +2405,33 @@ export default class GoTrueClient {
if (params.factorType === 'totp' && data?.totp?.qr_code) {
data.totp.qr_code = `data:image/svg+xml;utf-8,${data.totp.qr_code}`
}
if (params.factorType === 'webauthn' && params.useMultiStep && data.type === 'webauthn') {
const factorId = data.id
const { data: challengeData, error: challengeError } = await this._challenge({ factorId })
if (challengeError) {
return { data: null, error: challengeError }
}
if (!challengeData) {
return { data: null, error: new Error('Challenge data or options are null') }
}
if (!(challengeData.type === 'webauthn' && 'options' in challengeData)) {
return { data: null, error: new Error('Invalid challenge data for WebAuthn') }
}
try {
const publicKey = await navigator.credentials.create(
challengeData.options as CredentialCreationOptions
)
if (!publicKey) {
return { data: null, error: new Error('Failed to create credentials') }
}
return await this._verify({ factorId, publicKey })
} catch (credentialError) {
return {
data: null,
error: new Error(`Credential creation failed: ${credentialError}`),
}
}
}

return { data, error: null }
})
Expand Down Expand Up @@ -2554,6 +2583,7 @@ export default class GoTrueClient {
}

try {
// TODO: This needs to chagne since ChallengeAndVerify is also for enroll
const publicKey = await navigator.credentials.get(challengeResponse.options)
// TODO: handle credential error
if (!publicKey) {
Expand Down

0 comments on commit 094582b

Please sign in to comment.