Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
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
25 changes: 9 additions & 16 deletions src/GoTrueApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CookieOptions,
User,
OpenIDConnectCredentials,
VerifyOTPParams
VerifyOTPParams,
} from './lib/types'
import { COOKIE_OPTIONS } from './lib/constants'
import { setCookies, getCookieString } from './lib/cookies'
Expand Down Expand Up @@ -337,7 +337,7 @@ export default class GoTrueApi {
}

/**
* @deprecated Send User supplied Mobile OTP to be verified
* @deprecated Use `verifyOTP` instead!
* @param phone The user's phone number WITH international prefix
* @param token token that user was sent to their mobile phone
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
Expand Down Expand Up @@ -374,26 +374,19 @@ export default class GoTrueApi {
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
*/
async verifyOTP(
{ email, phone, token, type }: VerifyOTPParams,
{ email, phone, token, type = 'sms' }: VerifyOTPParams,
options: {
redirectTo?: string
} = {}
): Promise<{ data: Session | User | null; error: ApiError | null }> {
try {
const headers = { ...this.headers }
const data = (email && type)
? await post(
this.fetch,
`${this.url}/verify`,
{ email, token, type: type, redirect_to: options.redirectTo },
{ headers }
)
: await post(
this.fetch,
`${this.url}/verify`,
{ phone, token, type: 'sms', redirect_to: options.redirectTo },
{ headers }
)
const data = await post(
this.fetch,
`${this.url}/verify`,
{ email, phone, token, type, redirect_to: options.redirectTo },
{ headers }
)
const session = { ...data }
if (session.expires_in) session.expires_at = expiresAt(data.expires_in)
return { data: session, error: null }
Expand Down
4 changes: 2 additions & 2 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export default class GoTrueClient {
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
*/
async verifyOTP(
{ email, phone, token, type = 'sms' }: VerifyOTPParams,
params: VerifyOTPParams,
options: {
redirectTo?: string
} = {}
Expand All @@ -277,7 +277,7 @@ export default class GoTrueClient {
try {
this._removeSession()

const { data, error } = await this.api.verifyOTP({ email, phone, token, type }, options)
const { data, error } = await this.api.verifyOTP(params, options)

if (error) {
throw error
Expand Down
24 changes: 13 additions & 11 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,21 @@ export interface UserCredentials {
oidc?: OpenIDConnectCredentials
}

export interface VerifyOTPParams {
email?: string
phone?: string
export type VerifyOTPParams = VerifyMobileOTPParams | VerifyEmailOTPParams
export interface VerifyMobileOTPParams {
email?: undefined
phone: string
token: string
type?: MobileOTPType
}
export interface VerifyEmailOTPParams {
email: string
phone?: undefined
token: string
type?: OTPType
type: EmailOTPType
}
export type OTPType =
| 'signup'
| 'invite'
| 'magiclink'
| 'recover'
| 'email_change'
| 'sms'
export type MobileOTPType = 'sms'
export type EmailOTPType = 'signup' | 'invite' | 'magiclink' | 'recover' | 'email_change'
Comment on lines +201 to +202
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kangmingtay can you double check that this separation is correct?

Copy link
Member

Choose a reason for hiding this comment

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

hmm seems like it should be 'recovery' instead of 'recover' (https://github.com/supabase/gotrue/blob/master/api/verify.go#L24)


export interface OpenIDConnectCredentials {
id_token: string
Expand Down
19 changes: 8 additions & 11 deletions test/GoTrueApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
clientApiAutoConfirmOffSignupsEnabledClient,
serviceRoleApiClient,
serviceRoleApiClientWithSms,
authClient,
clientApiAutoConfirmDisabledClient,
} from './lib/clients'

Expand Down Expand Up @@ -531,30 +530,28 @@ describe('GoTrueApi', () => {
test('verifyOTP() with invalid email', async () => {
const { email } = mockUserCredentials()
const otp = mockVerificationOTP()
const { data, error } = await serviceRoleApiClientWithSms.verifyOTP({
email: `${email}-@invalid`,
token: otp,
type: 'signup'
const { data, error } = await serviceRoleApiClientWithSms.verifyOTP({
email: `${email}-@invalid`,
token: otp,
type: 'signup',
})

expect(data).toBeNull()
expect(error?.status).toEqual(422)
expect(error?.message).toEqual('Invalid email format')

})
test('verifyOTP() with invalid phone', async () => {
const { phone } = mockUserCredentials()
const otp = mockVerificationOTP()
const { data, error } = await serviceRoleApiClientWithSms.verifyOTP({
phone: `${phone}-invalid`,
token: otp,
type: 'sms'
const { data, error } = await serviceRoleApiClientWithSms.verifyOTP({
phone: `${phone}-invalid`,
token: otp,
type: 'sms',
})

expect(data).toBeNull()
expect(error?.status).toEqual(422)
expect(error?.message).toEqual('Invalid phone number format')

})
})
})
Expand Down