@supabase/auth-js 2.116.0-canary.2 → 2.116.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/main/GoTrueClient.d.ts +20 -0
  2. package/dist/main/GoTrueClient.d.ts.map +1 -1
  3. package/dist/main/GoTrueClient.js +170 -1
  4. package/dist/main/GoTrueClient.js.map +1 -1
  5. package/dist/main/lib/error-codes.d.ts +1 -1
  6. package/dist/main/lib/error-codes.d.ts.map +1 -1
  7. package/dist/main/lib/helpers.d.ts +3 -0
  8. package/dist/main/lib/helpers.d.ts.map +1 -1
  9. package/dist/main/lib/helpers.js +6 -0
  10. package/dist/main/lib/helpers.js.map +1 -1
  11. package/dist/main/lib/types.d.ts +352 -4
  12. package/dist/main/lib/types.d.ts.map +1 -1
  13. package/dist/main/lib/types.js +2 -1
  14. package/dist/main/lib/types.js.map +1 -1
  15. package/dist/main/lib/version.d.ts +1 -1
  16. package/dist/main/lib/version.d.ts.map +1 -1
  17. package/dist/main/lib/version.js +1 -1
  18. package/dist/main/lib/version.js.map +1 -1
  19. package/dist/module/GoTrueClient.d.ts +20 -0
  20. package/dist/module/GoTrueClient.d.ts.map +1 -1
  21. package/dist/module/GoTrueClient.js +171 -2
  22. package/dist/module/GoTrueClient.js.map +1 -1
  23. package/dist/module/lib/error-codes.d.ts +1 -1
  24. package/dist/module/lib/error-codes.d.ts.map +1 -1
  25. package/dist/module/lib/helpers.d.ts +3 -0
  26. package/dist/module/lib/helpers.d.ts.map +1 -1
  27. package/dist/module/lib/helpers.js +5 -0
  28. package/dist/module/lib/helpers.js.map +1 -1
  29. package/dist/module/lib/types.d.ts +352 -4
  30. package/dist/module/lib/types.d.ts.map +1 -1
  31. package/dist/module/lib/types.js +2 -1
  32. package/dist/module/lib/types.js.map +1 -1
  33. package/dist/module/lib/version.d.ts +1 -1
  34. package/dist/module/lib/version.d.ts.map +1 -1
  35. package/dist/module/lib/version.js +1 -1
  36. package/dist/module/lib/version.js.map +1 -1
  37. package/dist/tsconfig.module.tsbuildinfo +1 -1
  38. package/dist/tsconfig.tsbuildinfo +1 -1
  39. package/package.json +1 -1
  40. package/src/GoTrueClient.ts +214 -1
  41. package/src/lib/error-codes.ts +4 -0
  42. package/src/lib/helpers.ts +10 -0
  43. package/src/lib/types.ts +373 -3
  44. package/src/lib/version.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supabase/auth-js",
3
- "version": "2.116.0-canary.2",
3
+ "version": "2.116.0",
4
4
  "private": false,
5
5
  "description": "Official SDK for Supabase Auth",
6
6
  "keywords": [
@@ -39,9 +39,11 @@ import {
39
39
  import {
40
40
  appendFlowIdToRedirectTo,
41
41
  assertPasskeyExperimentalEnabled,
42
+ assertRecoveryCodesExperimentalEnabled,
42
43
  decodeJWT,
43
44
  deepClone,
44
45
  Deferred,
46
+ expiresAt,
45
47
  generateCallbackId,
46
48
  getAlgorithm,
47
49
  getCodeChallengeAndMethod,
@@ -84,6 +86,8 @@ import type {
84
86
  AuthMFAEnrollWebauthnResponse,
85
87
  AuthMFAGetAuthenticatorAssuranceLevelResponse,
86
88
  AuthMFAListFactorsResponse,
89
+ AuthMFARecoveryCodesGenerateResponse,
90
+ AuthMFARecoveryCodesStatusResponse,
87
91
  AuthMFAUnenrollResponse,
88
92
  AuthMFAVerifyResponse,
89
93
  AuthOtpResponse,
@@ -111,6 +115,8 @@ import type {
111
115
  MFAEnrollPhoneParams,
112
116
  MFAEnrollTOTPParams,
113
117
  MFAEnrollWebauthnParams,
118
+ MFARecoveryCodesGenerateParams,
119
+ MFARecoveryCodesVerifyParams,
114
120
  MFAUnenrollParams,
115
121
  MFAVerifyParams,
116
122
  MFAVerifyPhoneParams,
@@ -484,6 +490,13 @@ export default class GoTrueClient {
484
490
  challengeAndVerify: this._challengeAndVerify.bind(this),
485
491
  getAuthenticatorAssuranceLevel: this._getAuthenticatorAssuranceLevel.bind(this),
486
492
  webauthn: new WebAuthnApi(this),
493
+ recoveryCodes: {
494
+ getStatus: this._getRecoveryCodesStatus.bind(this),
495
+ generate: this._generateRecoveryCodes.bind(this),
496
+ verify: this._verifyRecoveryCode.bind(this),
497
+ regenerate: this._regenerateRecoveryCodes.bind(this),
498
+ unenroll: this._unenrollRecoveryCodes.bind(this),
499
+ },
487
500
  }
488
501
 
489
502
  this.oauth = {
@@ -6046,12 +6059,18 @@ export default class GoTrueClient {
6046
6059
  phone: [],
6047
6060
  totp: [],
6048
6061
  webauthn: [],
6062
+ recovery_code: [],
6049
6063
  }
6050
6064
 
6051
6065
  // loop over the factors ONCE
6052
6066
  for (const factor of user?.factors ?? []) {
6053
6067
  data.all.push(factor)
6054
- if (factor.status === 'verified') {
6068
+ // Types that are not recognized are pushed to `all` bucket.
6069
+ if (
6070
+ factor.status === 'verified' &&
6071
+ factor.factor_type in data &&
6072
+ Array.isArray(data[factor.factor_type])
6073
+ ) {
6055
6074
  ;(data[factor.factor_type] as (typeof factor)[]).push(factor)
6056
6075
  }
6057
6076
  }
@@ -6143,6 +6162,200 @@ export default class GoTrueClient {
6143
6162
  return { data: { currentLevel, nextLevel, currentAuthenticationMethods }, error: null }
6144
6163
  }
6145
6164
 
6165
+ /**
6166
+ * {@link AuthMFARecoveryCodesApi#getStatus}
6167
+ */
6168
+ private async _getRecoveryCodesStatus(): Promise<AuthMFARecoveryCodesStatusResponse> {
6169
+ assertRecoveryCodesExperimentalEnabled(this.experimental)
6170
+ try {
6171
+ return await this._useSession(async (result) => {
6172
+ const { data: sessionData, error: sessionError } = result
6173
+ if (sessionError) {
6174
+ return this._returnResult({ data: null, error: sessionError })
6175
+ }
6176
+
6177
+ const { data, error } = await _request(
6178
+ this.fetch,
6179
+ 'GET',
6180
+ `${this.url}/factors/recovery-codes`,
6181
+ {
6182
+ headers: this.headers,
6183
+ jwt: sessionData?.session?.access_token,
6184
+ }
6185
+ )
6186
+ if (error) {
6187
+ return this._returnResult({ data: null, error })
6188
+ }
6189
+
6190
+ return this._returnResult({ data, error: null })
6191
+ })
6192
+ } catch (error) {
6193
+ if (isAuthError(error)) {
6194
+ return this._returnResult({ data: null, error })
6195
+ }
6196
+ throw error
6197
+ }
6198
+ }
6199
+
6200
+ /**
6201
+ * {@link AuthMFARecoveryCodesApi#generate}
6202
+ */
6203
+ private async _generateRecoveryCodes(
6204
+ params?: MFARecoveryCodesGenerateParams
6205
+ ): Promise<AuthMFARecoveryCodesGenerateResponse> {
6206
+ assertRecoveryCodesExperimentalEnabled(this.experimental)
6207
+ try {
6208
+ return await this._useSession(async (result) => {
6209
+ const { data: sessionData, error: sessionError } = result
6210
+ if (sessionError) {
6211
+ return this._returnResult({ data: null, error: sessionError })
6212
+ }
6213
+
6214
+ const { data, error } = await _request(
6215
+ this.fetch,
6216
+ 'POST',
6217
+ `${this.url}/factors/recovery-codes`,
6218
+ {
6219
+ // The body is optional server-side; only send one when a name was given.
6220
+ body: params?.friendlyName ? { friendly_name: params.friendlyName } : undefined,
6221
+ headers: this.headers,
6222
+ jwt: sessionData?.session?.access_token,
6223
+ }
6224
+ )
6225
+ if (error) {
6226
+ return this._returnResult({ data: null, error })
6227
+ }
6228
+
6229
+ return this._returnResult({ data, error: null })
6230
+ })
6231
+ } catch (error) {
6232
+ if (isAuthError(error)) {
6233
+ return this._returnResult({ data: null, error })
6234
+ }
6235
+ throw error
6236
+ }
6237
+ }
6238
+
6239
+ /**
6240
+ * {@link AuthMFARecoveryCodesApi#verify}
6241
+ */
6242
+ private async _verifyRecoveryCode(
6243
+ params: MFARecoveryCodesVerifyParams
6244
+ ): Promise<AuthMFAVerifyResponse> {
6245
+ assertRecoveryCodesExperimentalEnabled(this.experimental)
6246
+ const run = async (): Promise<AuthMFAVerifyResponse> => {
6247
+ try {
6248
+ return await this._useSession(async (result) => {
6249
+ const { data: sessionData, error: sessionError } = result
6250
+ if (sessionError) {
6251
+ return this._returnResult({ data: null, error: sessionError })
6252
+ }
6253
+
6254
+ const { data, error } = await _request(
6255
+ this.fetch,
6256
+ 'POST',
6257
+ `${this.url}/factors/recovery-codes/verify`,
6258
+ {
6259
+ body: { code: params.code },
6260
+ headers: this.headers,
6261
+ jwt: sessionData?.session?.access_token,
6262
+ }
6263
+ )
6264
+ if (error) {
6265
+ return this._returnResult({ data: null, error })
6266
+ }
6267
+
6268
+ // A server-provided expires_at takes precedence over the computed one.
6269
+ const session: Session = { expires_at: expiresAt(data.expires_in), ...data }
6270
+ await this._saveSession(session)
6271
+ await this._notifyAllSubscribers('MFA_CHALLENGE_VERIFIED', session)
6272
+
6273
+ return this._returnResult({ data, error: null })
6274
+ })
6275
+ } catch (error) {
6276
+ if (isAuthError(error)) {
6277
+ return this._returnResult({ data: null, error })
6278
+ }
6279
+ throw error
6280
+ }
6281
+ }
6282
+
6283
+ if (this.lock != null) {
6284
+ return this._acquireLock(this.lockAcquireTimeout, run)
6285
+ }
6286
+ return run()
6287
+ }
6288
+
6289
+ /**
6290
+ * {@link AuthMFARecoveryCodesApi#regenerate}
6291
+ */
6292
+ private async _regenerateRecoveryCodes(): Promise<AuthMFARecoveryCodesGenerateResponse> {
6293
+ assertRecoveryCodesExperimentalEnabled(this.experimental)
6294
+ try {
6295
+ return await this._useSession(async (result) => {
6296
+ const { data: sessionData, error: sessionError } = result
6297
+ if (sessionError) {
6298
+ return this._returnResult({ data: null, error: sessionError })
6299
+ }
6300
+
6301
+ const { data, error } = await _request(
6302
+ this.fetch,
6303
+ 'POST',
6304
+ `${this.url}/factors/recovery-codes/regenerate`,
6305
+ {
6306
+ headers: this.headers,
6307
+ jwt: sessionData?.session?.access_token,
6308
+ }
6309
+ )
6310
+ if (error) {
6311
+ return this._returnResult({ data: null, error })
6312
+ }
6313
+
6314
+ return this._returnResult({ data, error: null })
6315
+ })
6316
+ } catch (error) {
6317
+ if (isAuthError(error)) {
6318
+ return this._returnResult({ data: null, error })
6319
+ }
6320
+ throw error
6321
+ }
6322
+ }
6323
+
6324
+ /**
6325
+ * {@link AuthMFARecoveryCodesApi#unenroll}
6326
+ */
6327
+ private async _unenrollRecoveryCodes(): Promise<AuthMFAUnenrollResponse> {
6328
+ assertRecoveryCodesExperimentalEnabled(this.experimental)
6329
+ try {
6330
+ return await this._useSession(async (result) => {
6331
+ const { data: sessionData, error: sessionError } = result
6332
+ if (sessionError) {
6333
+ return this._returnResult({ data: null, error: sessionError })
6334
+ }
6335
+
6336
+ const { data, error } = await _request(
6337
+ this.fetch,
6338
+ 'DELETE',
6339
+ `${this.url}/factors/recovery-codes`,
6340
+ {
6341
+ headers: this.headers,
6342
+ jwt: sessionData?.session?.access_token,
6343
+ }
6344
+ )
6345
+ if (error) {
6346
+ return this._returnResult({ data: null, error })
6347
+ }
6348
+
6349
+ return this._returnResult({ data, error: null })
6350
+ })
6351
+ } catch (error) {
6352
+ if (isAuthError(error)) {
6353
+ return this._returnResult({ data: null, error })
6354
+ }
6355
+ throw error
6356
+ }
6357
+ }
6358
+
6146
6359
  /**
6147
6360
  * Retrieves details about an OAuth authorization request.
6148
6361
  * Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
@@ -84,6 +84,10 @@ export type ErrorCode =
84
84
  | 'mfa_totp_verify_not_enabled'
85
85
  | 'mfa_webauthn_enroll_not_enabled'
86
86
  | 'mfa_webauthn_verify_not_enabled'
87
+ | 'mfa_recovery_codes_enroll_not_enabled'
88
+ | 'mfa_recovery_codes_verify_not_enabled'
89
+ | 'mfa_recovery_codes_locked'
90
+ | 'mfa_recovery_codes_sole_factor'
87
91
  | 'mfa_verified_factor_exists'
88
92
  | 'invalid_credentials'
89
93
  | 'email_address_not_authorized'
@@ -577,6 +577,16 @@ export function assertPasskeyExperimentalEnabled(experimental: { passkey?: boole
577
577
  }
578
578
  }
579
579
 
580
+ export function assertRecoveryCodesExperimentalEnabled(experimental: {
581
+ recoveryCodes?: boolean
582
+ }): void {
583
+ if (!experimental.recoveryCodes) {
584
+ throw new Error(
585
+ '@supabase/auth-js: the MFA recovery codes API is experimental and disabled by default. Enable it by passing `auth: { experimental: { recoveryCodes: true } }` to createClient (or to the GoTrueClient constructor).'
586
+ )
587
+ }
588
+ }
589
+
580
590
  export function userNotAvailableProxy(): User {
581
591
  const proxyTarget = {} as User
582
592