Skip to content

Commit

Permalink
feat: return messageId when using otp (#706)
Browse files Browse the repository at this point in the history
When GoTrue returns a `message_id` it should be made available to OTP
APIs.

See:
- supabase/auth#1145
  • Loading branch information
hf authored Jun 19, 2023
1 parent 8aaa6ac commit b3a6ff4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
AuthChangeEvent,
AuthResponse,
AuthTokenResponse,
AuthOtpResponse,
CallRefreshTokenResult,
GoTrueClientOptions,
InitializeResult,
Expand Down Expand Up @@ -515,7 +516,7 @@ export default class GoTrueClient {
* at this time.
* This method supports PKCE when an email is passed.
*/
async signInWithOtp(credentials: SignInWithPasswordlessCredentials): Promise<AuthResponse> {
async signInWithOtp(credentials: SignInWithPasswordlessCredentials): Promise<AuthOtpResponse> {
try {
await this._removeSession()

Expand Down Expand Up @@ -545,7 +546,7 @@ export default class GoTrueClient {
}
if ('phone' in credentials) {
const { phone, options } = credentials
const { error } = await _request(this.fetch, 'POST', `${this.url}/otp`, {
const { data, error } = await _request(this.fetch, 'POST', `${this.url}/otp`, {
headers: this.headers,
body: {
phone,
Expand All @@ -555,7 +556,7 @@ export default class GoTrueClient {
channel: options?.channel ?? 'sms',
},
})
return { data: { user: null, session: null }, error }
return { data: { user: null, session: null, messageId: data?.message_id }, error }
}
throw new AuthInvalidCredentialsError('You must provide either an email or phone number.')
} catch (error) {
Expand Down Expand Up @@ -680,7 +681,7 @@ export default class GoTrueClient {
/**
* Resends an existing signup confirmation email, email change email, SMS OTP or phone change OTP.
*/
async resend(credentials: ResendParams): Promise<AuthResponse> {
async resend(credentials: ResendParams): Promise<AuthOtpResponse> {
try {
await this._removeSession()
const endpoint = `${this.url}/resend`
Expand All @@ -697,15 +698,15 @@ export default class GoTrueClient {
return { data: { user: null, session: null }, error }
} else if ('phone' in credentials) {
const { phone, type, options } = credentials
const { error } = await _request(this.fetch, 'POST', endpoint, {
const { data, error } = await _request(this.fetch, 'POST', endpoint, {
headers: this.headers,
body: {
phone,
type,
gotrue_meta_security: { captcha_token: options?.captchaToken },
},
})
return { data: { user: null, session: null }, error }
return { data: { user: null, session: null, messageId: data?.message_id }, error }
}
throw new AuthInvalidCredentialsError(
'You must provide either an email or phone number and a type'
Expand Down
15 changes: 15 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ export type AuthResponse =
error: AuthError
}

/**
* AuthOtpResponse is returned when OTP is used.
*
* {@see AuthRsponse}
*/
export type AuthOtpResponse =
| {
data: { user: null; session: null; messageId?: string | null }
error: null
}
| {
data: { user: null; session: null; messageId?: string | null }
error: AuthError
}

export type AuthTokenResponse =
| {
data: {
Expand Down

0 comments on commit b3a6ff4

Please sign in to comment.