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
33 changes: 32 additions & 1 deletion src/GoTrueApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CookieOptions,
User,
OpenIDConnectCredentials,
VerifyOTPParams,
} from './lib/types'
import { COOKIE_OPTIONS } from './lib/constants'
import { setCookies, getCookieString } from './lib/cookies'
Expand Down Expand Up @@ -336,7 +337,7 @@ export default class GoTrueApi {
}

/**
* 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 @@ -364,6 +365,36 @@ export default class GoTrueApi {
}
}

/**
* Send User supplied Email / Mobile OTP to be verified
* @param email The user's email address
* @param phone The user's phone number WITH international prefix
* @param token token that user was sent to their mobile phone
* @param type verification type that the otp is generated for
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
*/
async verifyOTP(
{ email, phone, token, type = 'sms' }: VerifyOTPParams,
options: {
redirectTo?: string
} = {}
): Promise<{ data: Session | User | null; error: ApiError | null }> {
try {
const headers = { ...this.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 }
} catch (e) {
return { data: null, error: e as ApiError }
}
}

/**
* Sends an invite link to an email address.
* @param email The email address of the user.
Expand Down
6 changes: 4 additions & 2 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,14 @@ export default class GoTrueClient {

/**
* Log in a user given a User supplied OTP received via mobile.
* @param email The user's email address.
* @param phone The user's phone number.
* @param token The user's password.
* @param type The user's verification type.
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
*/
async verifyOTP(
{ phone, token }: VerifyOTPParams,
params: VerifyOTPParams,
options: {
redirectTo?: string
} = {}
Expand All @@ -275,7 +277,7 @@ export default class GoTrueClient {
try {
this._removeSession()

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

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

export interface VerifyOTPParams {
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: EmailOTPType
}
export type MobileOTPType = 'sms'
export type EmailOTPType = 'signup' | 'invite' | 'magiclink' | 'recovery' | 'email_change'

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

import {
createNewUserWithEmail,
mockUserCredentials,
mockAppMetadata,
mockUserMetadata,
mockVerificationOTP,
} from './lib/utils'

import type { Session, User } from '../src/lib/types'
Expand Down Expand Up @@ -494,4 +496,63 @@ describe('GoTrueApi', () => {
})
})
})

describe('Email/Phone OTP Verification', () => {
describe('GoTrueClient verifyOTP()', () => {
test('verifyOTP() with non-existent phone number', async () => {
const { phone } = mockUserCredentials()
const otp = mockVerificationOTP()
const { user, error } = await clientApiAutoConfirmDisabledClient.verifyOTP({
phone: `${phone}`,
token: otp,
})

expect(user).toBeNull()
expect(error?.status).toEqual(404)
expect(error?.message).toEqual('User not found')
})

test('verifyOTP() with invalid phone number', async () => {
const { phone } = mockUserCredentials()
const otp = mockVerificationOTP()
const { user, error } = await clientApiAutoConfirmDisabledClient.verifyOTP({
phone: `${phone}-invalid`,
token: otp,
})

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

describe('GoTrueApi verifyOTP()', () => {
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',
})

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',
})

expect(data).toBeNull()
expect(error?.status).toEqual(422)
expect(error?.message).toEqual('Invalid phone number format')
})
})
})
})
4 changes: 4 additions & 0 deletions test/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export const mockUserCredentials = (
}
}

export const mockVerificationOTP = (): string => {
return Math.floor(100000 + Math.random() * 900000).toString()
}

export const mockUserMetadata = () => {
return {
profile_image: faker.image.avatar(),
Expand Down