Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Jul 21, 2024
1 parent 29fc62c commit 0f97967
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
49 changes: 31 additions & 18 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2341,25 +2341,38 @@ export default class GoTrueClient {
return { data: null, error: sessionError }
}

const { data, error } = await _request(this.fetch, 'POST', `${this.url}/factors`, {
body: {
friendly_name: params.friendlyName,
factor_type: params.factorType,
issuer: params.issuer,
},
headers: this.headers,
jwt: sessionData?.session?.access_token,
})

if (error) {
return { data: null, error }
}

if (data?.totp?.qr_code) {
data.totp.qr_code = `data:image/svg+xml;utf-8,${data.totp.qr_code}`
if (params.factorType === 'sms') {
const { data, error } = await _request(this.fetch, 'POST', `${this.url}/factors`, {
body: {
friendly_name: params.friendlyName,
factor_type: params.factorType,
phone_number: params.phoneNumber,
},
headers: this.headers,
jwt: sessionData?.session?.access_token,
})
if (error) {
return { data: null, error }
}
return { data, error: null }
} else {
const { data, error } = await _request(this.fetch, 'POST', `${this.url}/factors`, {
body: {
friendly_name: params.friendlyName,
factor_type: params.factorType,
issuer: params.issuer,
},
headers: this.headers,
jwt: sessionData?.session?.access_token,
})
if (error) {
return { data: null, error }
}
if (data?.totp?.qr_code) {
data.totp.qr_code = `data:image/svg+xml;utf-8,${data.totp.qr_code}`
}
return { data, error: null }
}

return { data, error: null }
})
} catch (error) {
if (isAuthError(error)) {
Expand Down
43 changes: 35 additions & 8 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,18 +805,29 @@ export type GenerateLinkType =
| 'email_change_current'
| 'email_change_new'

export type MFAEnrollParams = {
/** The type of factor being enrolled. */
factorType: 'totp'
/** Domain which the user is enrolled with. */
issuer?: string
/** Human readable name assigned to the factor. */
friendlyName?: string
}
export type MFAEnrollParams =
| {
/** The type of factor being enrolled. */
factorType: 'totp'
/** Domain which the user is enrolled with. */
issuer?: string
/** Human readable name assigned to the factor. */
friendlyName?: string
}
| {
/** The type of factor being enrolled. */
factorType: 'sms'
/** Human readable name assigned to the factor. */
friendlyName?: string
/** Phone number associated with a factor */
phoneNumber: string
}

export type MFAUnenrollParams = {
/** ID of the factor being unenrolled. */
factorId: string
/** Phone number of the SMS Factor being enrolled */
phoneNumber: string
}

export type MFAVerifyParams = {
Expand Down Expand Up @@ -897,6 +908,22 @@ export type AuthMFAEnrollResponse =
}
error: null
}
| {
data: {
/** ID of the factor that was just enrolled (in an unverified state). */
id: string

/** Type of MFA factor. Only `totp` supported for now. */
type: 'sms'

/** Friendly name of the factor, useful for distinguishing between factors **/
friendly_name?: string

/** Phone number of the MFA factor. Used to send SMS-es */
phone_number: string
}
error: null
}
| {
data: null
error: AuthError
Expand Down

0 comments on commit 0f97967

Please sign in to comment.