Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move MFA sub types to internal file #964

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ import type {
LockFunc,
UserIdentity,
SignInAnonymouslyCredentials,
} from './lib/types'
import {
MFAEnrollTOTPParams,
AuthMFAEnrollTOTPResponse,
AuthMFAEnrollErrorResponse,
MFAEnrollPhoneParams,
AuthMFAEnrollTOTPResponse,
AuthMFAEnrollPhoneResponse,
} from './lib/types'
} from './lib/internal-types'

polyfillGlobalThis() // Make "globalThis" available

Expand Down Expand Up @@ -313,9 +314,7 @@ export default class GoTrueClient {
if (error) {
this._debug('#_initialize()', 'error detecting session from URL', error)

if (
error?.code === 'identity_already_exists'
) {
if (error?.code === 'identity_already_exists') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettier

return { error }
}

Expand Down Expand Up @@ -2355,12 +2354,8 @@ export default class GoTrueClient {
/**
* {@see GoTrueMFAApi#enroll}
*/
private async _enroll(
params: MFAEnrollTOTPParams
): Promise<AuthMFAEnrollTOTPResponse | AuthMFAEnrollErrorResponse>
private async _enroll(
params: MFAEnrollPhoneParams
): Promise<AuthMFAEnrollPhoneResponse | AuthMFAEnrollErrorResponse>
private async _enroll(params: MFAEnrollTOTPParams): Promise<AuthMFAEnrollTOTPResponse>
private async _enroll(params: MFAEnrollPhoneParams): Promise<AuthMFAEnrollPhoneResponse>
private async _enroll(params: MFAEnrollParams): Promise<AuthMFAEnrollResponse> {
try {
return await this._useSession(async (result) => {
Expand Down
75 changes: 75 additions & 0 deletions src/lib/internal-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { AuthError } from './errors'

export type MFAEnrollTOTPParams = {
/** 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 MFAEnrollPhoneParams = {
/** The type of factor being enrolled. */
factorType: 'phone'
/** Human readable name assigned to the factor. */
friendlyName?: string
/** Phone number associated with a factor. Number should conform to E.164 format */
phone: string
}

export type AuthMFAEnrollTOTPResponse =
| {
data: {
/** ID of the factor that was just enrolled (in an unverified state). */
id: string

/** Type of MFA factor.*/
type: 'totp'

/** TOTP enrollment information. */
totp: {
/** Contains a QR code encoding the authenticator URI. You can
* convert it to a URL by prepending `data:image/svg+xml;utf-8,` to
* the value. Avoid logging this value to the console. */
qr_code: string

/** The TOTP secret (also encoded in the QR code). Show this secret
* in a password-style field to the user, in case they are unable to
* scan the QR code. Avoid logging this value to the console. */
secret: string

/** The authenticator URI encoded within the QR code, should you need
* to use it. Avoid loggin this value to the console. */
uri: string
}
/** Friendly name of the factor, useful for distinguishing between factors **/
friendly_name?: string
}
error: null
}
| {
data: null
error: AuthError
}

export type AuthMFAEnrollPhoneResponse =
| {
data: {
/** ID of the factor that was just enrolled (in an unverified state). */
id: string

/** Type of MFA factor. */
type: 'phone'

/** Friendly name of the factor, useful for distinguishing between factors **/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only export the types file so this shouldn't get exposed

friendly_name?: string

/** Phone number of the MFA factor in E.164 format. Used to send messages */
phone: string
}
error: null
}
| {
data: null
error: AuthError
}
84 changes: 9 additions & 75 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { AuthError } from './errors'
import { Fetch } from './fetch'
import {
MFAEnrollTOTPParams,
MFAEnrollPhoneParams,
AuthMFAEnrollTOTPResponse,
AuthMFAEnrollPhoneResponse,
} from './internal-types'

/** One of the providers supported by GoTrue. */
export type Provider =
Expand Down Expand Up @@ -800,22 +806,6 @@ export type GenerateLinkType =
| 'email_change_current'
| 'email_change_new'

export type MFAEnrollTOTPParams = {
/** 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 MFAEnrollPhoneParams = {
/** The type of factor being enrolled. */
factorType: 'phone'
/** Human readable name assigned to the factor. */
friendlyName?: string
/** Phone number associated with a factor. Number should conform to E.164 format */
phone: string
}
export type MFAEnrollParams = MFAEnrollTOTPParams | MFAEnrollPhoneParams

export type MFAUnenrollParams = {
Expand Down Expand Up @@ -873,59 +863,7 @@ export type AuthMFAVerifyResponse =
error: AuthError
}

export type AuthMFAEnrollTOTPResponse = {
data: {
/** ID of the factor that was just enrolled (in an unverified state). */
id: string

/** Type of MFA factor.*/
type: 'totp'

/** TOTP enrollment information. */
totp: {
/** Contains a QR code encoding the authenticator URI. You can
* convert it to a URL by prepending `data:image/svg+xml;utf-8,` to
* the value. Avoid logging this value to the console. */
qr_code: string

/** The TOTP secret (also encoded in the QR code). Show this secret
* in a password-style field to the user, in case they are unable to
* scan the QR code. Avoid logging this value to the console. */
secret: string

/** The authenticator URI encoded within the QR code, should you need
* to use it. Avoid loggin this value to the console. */
uri: string
}
/** Friendly name of the factor, useful for distinguishing between factors **/
friendly_name?: string
}
error: null
}
export type AuthMFAEnrollPhoneResponse = {
data: {
/** ID of the factor that was just enrolled (in an unverified state). */
id: string

/** Type of MFA factor. */
type: 'phone'

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

/** Phone number of the MFA factor in E.164 format. Used to send messages */
phone: string
}
error: null
}
export type AuthMFAEnrollErrorResponse = {
data: null
error: AuthError
}
export type AuthMFAEnrollResponse =
| AuthMFAEnrollTOTPResponse
| AuthMFAEnrollPhoneResponse
| AuthMFAEnrollErrorResponse
export type AuthMFAEnrollResponse = AuthMFAEnrollTOTPResponse | AuthMFAEnrollPhoneResponse

export type AuthMFAUnenrollResponse =
| {
Expand Down Expand Up @@ -1010,12 +948,8 @@ export interface GoTrueMFAApi {
* Upon verifying a factor, all other sessions are logged out and the current session's authenticator level is promoted to `aal2`.
*
*/
enroll(
params: MFAEnrollTOTPParams
): Promise<AuthMFAEnrollTOTPResponse | AuthMFAEnrollErrorResponse>
enroll(
params: MFAEnrollPhoneParams
): Promise<AuthMFAEnrollPhoneResponse | AuthMFAEnrollErrorResponse>
enroll(params: MFAEnrollTOTPParams): Promise<AuthMFAEnrollTOTPResponse>
enroll(params: MFAEnrollPhoneParams): Promise<AuthMFAEnrollPhoneResponse>
enroll(params: MFAEnrollParams): Promise<AuthMFAEnrollResponse>

/**
Expand Down