@depup/supabase__auth-js 2.99.2-depup.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.
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/changes.json +5 -0
- package/dist/main/AuthAdminApi.d.ts +4 -0
- package/dist/main/AuthAdminApi.d.ts.map +1 -0
- package/dist/main/AuthAdminApi.js +7 -0
- package/dist/main/AuthAdminApi.js.map +1 -0
- package/dist/main/AuthClient.d.ts +4 -0
- package/dist/main/AuthClient.d.ts.map +1 -0
- package/dist/main/AuthClient.js +7 -0
- package/dist/main/AuthClient.js.map +1 -0
- package/dist/main/GoTrueAdminApi.d.ts +227 -0
- package/dist/main/GoTrueAdminApi.d.ts.map +1 -0
- package/dist/main/GoTrueAdminApi.js +596 -0
- package/dist/main/GoTrueAdminApi.js.map +1 -0
- package/dist/main/GoTrueClient.d.ts +783 -0
- package/dist/main/GoTrueClient.d.ts.map +1 -0
- package/dist/main/GoTrueClient.js +3029 -0
- package/dist/main/GoTrueClient.js.map +1 -0
- package/dist/main/index.d.ts +9 -0
- package/dist/main/index.d.ts.map +1 -0
- package/dist/main/index.js +20 -0
- package/dist/main/index.js.map +1 -0
- package/dist/main/lib/base64url.d.ts +76 -0
- package/dist/main/lib/base64url.d.ts.map +1 -0
- package/dist/main/lib/base64url.js +269 -0
- package/dist/main/lib/base64url.js.map +1 -0
- package/dist/main/lib/constants.d.ts +26 -0
- package/dist/main/lib/constants.d.ts.map +1 -0
- package/dist/main/lib/constants.js +31 -0
- package/dist/main/lib/constants.js.map +1 -0
- package/dist/main/lib/error-codes.d.ts +7 -0
- package/dist/main/lib/error-codes.d.ts.map +1 -0
- package/dist/main/lib/error-codes.js +3 -0
- package/dist/main/lib/error-codes.js.map +1 -0
- package/dist/main/lib/errors.d.ts +243 -0
- package/dist/main/lib/errors.d.ts.map +1 -0
- package/dist/main/lib/errors.js +289 -0
- package/dist/main/lib/errors.js.map +1 -0
- package/dist/main/lib/fetch.d.ts +34 -0
- package/dist/main/lib/fetch.d.ts.map +1 -0
- package/dist/main/lib/fetch.js +184 -0
- package/dist/main/lib/fetch.js.map +1 -0
- package/dist/main/lib/helpers.d.ts +91 -0
- package/dist/main/lib/helpers.d.ts.map +1 -0
- package/dist/main/lib/helpers.js +395 -0
- package/dist/main/lib/helpers.js.map +1 -0
- package/dist/main/lib/local-storage.d.ts +9 -0
- package/dist/main/lib/local-storage.d.ts.map +1 -0
- package/dist/main/lib/local-storage.js +21 -0
- package/dist/main/lib/local-storage.js.map +1 -0
- package/dist/main/lib/locks.d.ts +107 -0
- package/dist/main/lib/locks.d.ts.map +1 -0
- package/dist/main/lib/locks.js +314 -0
- package/dist/main/lib/locks.js.map +1 -0
- package/dist/main/lib/polyfills.d.ts +5 -0
- package/dist/main/lib/polyfills.d.ts.map +1 -0
- package/dist/main/lib/polyfills.js +29 -0
- package/dist/main/lib/polyfills.js.map +1 -0
- package/dist/main/lib/types.d.ts +1861 -0
- package/dist/main/lib/types.d.ts.map +1 -0
- package/dist/main/lib/types.js +23 -0
- package/dist/main/lib/types.js.map +1 -0
- package/dist/main/lib/version.d.ts +2 -0
- package/dist/main/lib/version.d.ts.map +1 -0
- package/dist/main/lib/version.js +11 -0
- package/dist/main/lib/version.js.map +1 -0
- package/dist/main/lib/web3/ethereum.d.ts +96 -0
- package/dist/main/lib/web3/ethereum.d.ts.map +1 -0
- package/dist/main/lib/web3/ethereum.js +66 -0
- package/dist/main/lib/web3/ethereum.js.map +1 -0
- package/dist/main/lib/web3/solana.d.ts +160 -0
- package/dist/main/lib/web3/solana.d.ts.map +1 -0
- package/dist/main/lib/web3/solana.js +4 -0
- package/dist/main/lib/web3/solana.js.map +1 -0
- package/dist/main/lib/webauthn.d.ts +276 -0
- package/dist/main/lib/webauthn.d.ts.map +1 -0
- package/dist/main/lib/webauthn.dom.d.ts +583 -0
- package/dist/main/lib/webauthn.dom.d.ts.map +1 -0
- package/dist/main/lib/webauthn.dom.js +4 -0
- package/dist/main/lib/webauthn.dom.js.map +1 -0
- package/dist/main/lib/webauthn.errors.d.ts +80 -0
- package/dist/main/lib/webauthn.errors.d.ts.map +1 -0
- package/dist/main/lib/webauthn.errors.js +265 -0
- package/dist/main/lib/webauthn.errors.js.map +1 -0
- package/dist/main/lib/webauthn.js +706 -0
- package/dist/main/lib/webauthn.js.map +1 -0
- package/dist/module/AuthAdminApi.d.ts +4 -0
- package/dist/module/AuthAdminApi.d.ts.map +1 -0
- package/dist/module/AuthAdminApi.js +4 -0
- package/dist/module/AuthAdminApi.js.map +1 -0
- package/dist/module/AuthClient.d.ts +4 -0
- package/dist/module/AuthClient.d.ts.map +1 -0
- package/dist/module/AuthClient.js +4 -0
- package/dist/module/AuthClient.js.map +1 -0
- package/dist/module/GoTrueAdminApi.d.ts +227 -0
- package/dist/module/GoTrueAdminApi.d.ts.map +1 -0
- package/dist/module/GoTrueAdminApi.js +593 -0
- package/dist/module/GoTrueAdminApi.js.map +1 -0
- package/dist/module/GoTrueClient.d.ts +783 -0
- package/dist/module/GoTrueClient.d.ts.map +1 -0
- package/dist/module/GoTrueClient.js +3026 -0
- package/dist/module/GoTrueClient.js.map +1 -0
- package/dist/module/index.d.ts +9 -0
- package/dist/module/index.d.ts.map +1 -0
- package/dist/module/index.js +9 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/lib/base64url.d.ts +76 -0
- package/dist/module/lib/base64url.d.ts.map +1 -0
- package/dist/module/lib/base64url.js +257 -0
- package/dist/module/lib/base64url.js.map +1 -0
- package/dist/module/lib/constants.d.ts +26 -0
- package/dist/module/lib/constants.d.ts.map +1 -0
- package/dist/module/lib/constants.js +28 -0
- package/dist/module/lib/constants.js.map +1 -0
- package/dist/module/lib/error-codes.d.ts +7 -0
- package/dist/module/lib/error-codes.d.ts.map +1 -0
- package/dist/module/lib/error-codes.js +2 -0
- package/dist/module/lib/error-codes.js.map +1 -0
- package/dist/module/lib/errors.d.ts +243 -0
- package/dist/module/lib/errors.d.ts.map +1 -0
- package/dist/module/lib/errors.js +266 -0
- package/dist/module/lib/errors.js.map +1 -0
- package/dist/module/lib/fetch.d.ts +34 -0
- package/dist/module/lib/fetch.d.ts.map +1 -0
- package/dist/module/lib/fetch.js +174 -0
- package/dist/module/lib/fetch.js.map +1 -0
- package/dist/module/lib/helpers.d.ts +91 -0
- package/dist/module/lib/helpers.d.ts.map +1 -0
- package/dist/module/lib/helpers.js +368 -0
- package/dist/module/lib/helpers.js.map +1 -0
- package/dist/module/lib/local-storage.d.ts +9 -0
- package/dist/module/lib/local-storage.d.ts.map +1 -0
- package/dist/module/lib/local-storage.js +18 -0
- package/dist/module/lib/local-storage.js.map +1 -0
- package/dist/module/lib/locks.d.ts +107 -0
- package/dist/module/lib/locks.d.ts.map +1 -0
- package/dist/module/lib/locks.js +306 -0
- package/dist/module/lib/locks.js.map +1 -0
- package/dist/module/lib/polyfills.d.ts +5 -0
- package/dist/module/lib/polyfills.d.ts.map +1 -0
- package/dist/module/lib/polyfills.js +26 -0
- package/dist/module/lib/polyfills.js.map +1 -0
- package/dist/module/lib/types.d.ts +1861 -0
- package/dist/module/lib/types.d.ts.map +1 -0
- package/dist/module/lib/types.js +20 -0
- package/dist/module/lib/types.js.map +1 -0
- package/dist/module/lib/version.d.ts +2 -0
- package/dist/module/lib/version.d.ts.map +1 -0
- package/dist/module/lib/version.js +8 -0
- package/dist/module/lib/version.js.map +1 -0
- package/dist/module/lib/web3/ethereum.d.ts +96 -0
- package/dist/module/lib/web3/ethereum.d.ts.map +1 -0
- package/dist/module/lib/web3/ethereum.js +60 -0
- package/dist/module/lib/web3/ethereum.js.map +1 -0
- package/dist/module/lib/web3/solana.d.ts +160 -0
- package/dist/module/lib/web3/solana.d.ts.map +1 -0
- package/dist/module/lib/web3/solana.js +3 -0
- package/dist/module/lib/web3/solana.js.map +1 -0
- package/dist/module/lib/webauthn.d.ts +276 -0
- package/dist/module/lib/webauthn.d.ts.map +1 -0
- package/dist/module/lib/webauthn.dom.d.ts +583 -0
- package/dist/module/lib/webauthn.dom.d.ts.map +1 -0
- package/dist/module/lib/webauthn.dom.js +3 -0
- package/dist/module/lib/webauthn.dom.js.map +1 -0
- package/dist/module/lib/webauthn.errors.d.ts +80 -0
- package/dist/module/lib/webauthn.errors.d.ts.map +1 -0
- package/dist/module/lib/webauthn.errors.js +257 -0
- package/dist/module/lib/webauthn.errors.js.map +1 -0
- package/dist/module/lib/webauthn.js +689 -0
- package/dist/module/lib/webauthn.js.map +1 -0
- package/dist/tsconfig.module.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +56 -0
- package/src/AuthAdminApi.ts +5 -0
- package/src/AuthClient.ts +5 -0
- package/src/GoTrueAdminApi.ts +723 -0
- package/src/GoTrueClient.ts +4078 -0
- package/src/index.ts +13 -0
- package/src/lib/base64url.ts +308 -0
- package/src/lib/constants.ts +34 -0
- package/src/lib/error-codes.ts +90 -0
- package/src/lib/errors.ts +324 -0
- package/src/lib/fetch.ts +283 -0
- package/src/lib/helpers.ts +463 -0
- package/src/lib/local-storage.ts +21 -0
- package/src/lib/locks.ts +375 -0
- package/src/lib/polyfills.ts +23 -0
- package/src/lib/types.ts +2229 -0
- package/src/lib/version.ts +7 -0
- package/src/lib/web3/ethereum.ts +184 -0
- package/src/lib/web3/solana.ts +186 -0
- package/src/lib/webauthn.dom.ts +636 -0
- package/src/lib/webauthn.errors.ts +317 -0
- package/src/lib/webauthn.ts +946 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { WeakPasswordReasons } from './types'
|
|
2
|
+
import { ErrorCode } from './error-codes'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base error thrown by Supabase Auth helpers.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { AuthError } from '@supabase/auth-js'
|
|
10
|
+
*
|
|
11
|
+
* throw new AuthError('Unexpected auth error', 500, 'unexpected')
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export class AuthError extends Error {
|
|
15
|
+
/**
|
|
16
|
+
* Error code associated with the error. Most errors coming from
|
|
17
|
+
* HTTP responses will have a code, though some errors that occur
|
|
18
|
+
* before a response is received will not have one present. In that
|
|
19
|
+
* case {@link #status} will also be undefined.
|
|
20
|
+
*/
|
|
21
|
+
code: ErrorCode | (string & {}) | undefined
|
|
22
|
+
|
|
23
|
+
/** HTTP status code that caused the error. */
|
|
24
|
+
status: number | undefined
|
|
25
|
+
|
|
26
|
+
protected __isAuthError = true
|
|
27
|
+
|
|
28
|
+
constructor(message: string, status?: number, code?: string) {
|
|
29
|
+
super(message)
|
|
30
|
+
this.name = 'AuthError'
|
|
31
|
+
this.status = status
|
|
32
|
+
this.code = code
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isAuthError(error: unknown): error is AuthError {
|
|
37
|
+
return typeof error === 'object' && error !== null && '__isAuthError' in error
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Error returned directly from the GoTrue REST API.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { AuthApiError } from '@supabase/auth-js'
|
|
46
|
+
*
|
|
47
|
+
* throw new AuthApiError('Invalid credentials', 400, 'invalid_credentials')
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export class AuthApiError extends AuthError {
|
|
51
|
+
status: number
|
|
52
|
+
|
|
53
|
+
constructor(message: string, status: number, code: string | undefined) {
|
|
54
|
+
super(message, status, code)
|
|
55
|
+
this.name = 'AuthApiError'
|
|
56
|
+
this.status = status
|
|
57
|
+
this.code = code
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isAuthApiError(error: unknown): error is AuthApiError {
|
|
62
|
+
return isAuthError(error) && error.name === 'AuthApiError'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Wraps non-standard errors so callers can inspect the root cause.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { AuthUnknownError } from '@supabase/auth-js'
|
|
71
|
+
*
|
|
72
|
+
* try {
|
|
73
|
+
* await someAuthCall()
|
|
74
|
+
* } catch (err) {
|
|
75
|
+
* throw new AuthUnknownError('Auth failed', err)
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export class AuthUnknownError extends AuthError {
|
|
80
|
+
originalError: unknown
|
|
81
|
+
|
|
82
|
+
constructor(message: string, originalError: unknown) {
|
|
83
|
+
super(message)
|
|
84
|
+
this.name = 'AuthUnknownError'
|
|
85
|
+
this.originalError = originalError
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Flexible error class used to create named auth errors at runtime.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* import { CustomAuthError } from '@supabase/auth-js'
|
|
95
|
+
*
|
|
96
|
+
* throw new CustomAuthError('My custom auth error', 'MyAuthError', 400, 'custom_code')
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export class CustomAuthError extends AuthError {
|
|
100
|
+
name: string
|
|
101
|
+
status: number
|
|
102
|
+
|
|
103
|
+
constructor(message: string, name: string, status: number, code: string | undefined) {
|
|
104
|
+
super(message, status, code)
|
|
105
|
+
this.name = name
|
|
106
|
+
this.status = status
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Error thrown when an operation requires a session but none is present.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* import { AuthSessionMissingError } from '@supabase/auth-js'
|
|
116
|
+
*
|
|
117
|
+
* throw new AuthSessionMissingError()
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export class AuthSessionMissingError extends CustomAuthError {
|
|
121
|
+
constructor() {
|
|
122
|
+
super('Auth session missing!', 'AuthSessionMissingError', 400, undefined)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function isAuthSessionMissingError(error: any): error is AuthSessionMissingError {
|
|
127
|
+
return isAuthError(error) && error.name === 'AuthSessionMissingError'
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Error thrown when the token response is malformed.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* import { AuthInvalidTokenResponseError } from '@supabase/auth-js'
|
|
136
|
+
*
|
|
137
|
+
* throw new AuthInvalidTokenResponseError()
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export class AuthInvalidTokenResponseError extends CustomAuthError {
|
|
141
|
+
constructor() {
|
|
142
|
+
super('Auth session or user missing', 'AuthInvalidTokenResponseError', 500, undefined)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Error thrown when email/password credentials are invalid.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* import { AuthInvalidCredentialsError } from '@supabase/auth-js'
|
|
152
|
+
*
|
|
153
|
+
* throw new AuthInvalidCredentialsError('Email or password is incorrect')
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export class AuthInvalidCredentialsError extends CustomAuthError {
|
|
157
|
+
constructor(message: string) {
|
|
158
|
+
super(message, 'AuthInvalidCredentialsError', 400, undefined)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Error thrown when implicit grant redirects contain an error.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* import { AuthImplicitGrantRedirectError } from '@supabase/auth-js'
|
|
168
|
+
*
|
|
169
|
+
* throw new AuthImplicitGrantRedirectError('OAuth redirect failed', {
|
|
170
|
+
* error: 'access_denied',
|
|
171
|
+
* code: 'oauth_error',
|
|
172
|
+
* })
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
export class AuthImplicitGrantRedirectError extends CustomAuthError {
|
|
176
|
+
details: { error: string; code: string } | null = null
|
|
177
|
+
constructor(message: string, details: { error: string; code: string } | null = null) {
|
|
178
|
+
super(message, 'AuthImplicitGrantRedirectError', 500, undefined)
|
|
179
|
+
this.details = details
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
toJSON() {
|
|
183
|
+
return {
|
|
184
|
+
name: this.name,
|
|
185
|
+
message: this.message,
|
|
186
|
+
status: this.status,
|
|
187
|
+
details: this.details,
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function isAuthImplicitGrantRedirectError(
|
|
193
|
+
error: any
|
|
194
|
+
): error is AuthImplicitGrantRedirectError {
|
|
195
|
+
return isAuthError(error) && error.name === 'AuthImplicitGrantRedirectError'
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Error thrown during PKCE code exchanges.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* import { AuthPKCEGrantCodeExchangeError } from '@supabase/auth-js'
|
|
204
|
+
*
|
|
205
|
+
* throw new AuthPKCEGrantCodeExchangeError('PKCE exchange failed')
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
export class AuthPKCEGrantCodeExchangeError extends CustomAuthError {
|
|
209
|
+
details: { error: string; code: string } | null = null
|
|
210
|
+
|
|
211
|
+
constructor(message: string, details: { error: string; code: string } | null = null) {
|
|
212
|
+
super(message, 'AuthPKCEGrantCodeExchangeError', 500, undefined)
|
|
213
|
+
this.details = details
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
toJSON() {
|
|
217
|
+
return {
|
|
218
|
+
name: this.name,
|
|
219
|
+
message: this.message,
|
|
220
|
+
status: this.status,
|
|
221
|
+
details: this.details,
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Error thrown when the PKCE code verifier is not found in storage.
|
|
228
|
+
* This typically happens when the auth flow was initiated in a different
|
|
229
|
+
* browser, device, or the storage was cleared.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* import { AuthPKCECodeVerifierMissingError } from '@supabase/auth-js'
|
|
234
|
+
*
|
|
235
|
+
* throw new AuthPKCECodeVerifierMissingError()
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
export class AuthPKCECodeVerifierMissingError extends CustomAuthError {
|
|
239
|
+
constructor() {
|
|
240
|
+
super(
|
|
241
|
+
'PKCE code verifier not found in storage. ' +
|
|
242
|
+
'This can happen if the auth flow was initiated in a different browser or device, ' +
|
|
243
|
+
'or if the storage was cleared. For SSR frameworks (Next.js, SvelteKit, etc.), ' +
|
|
244
|
+
'use @supabase/ssr on both the server and client to store the code verifier in cookies.',
|
|
245
|
+
'AuthPKCECodeVerifierMissingError',
|
|
246
|
+
400,
|
|
247
|
+
'pkce_code_verifier_not_found'
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function isAuthPKCECodeVerifierMissingError(
|
|
253
|
+
error: unknown
|
|
254
|
+
): error is AuthPKCECodeVerifierMissingError {
|
|
255
|
+
return isAuthError(error) && error.name === 'AuthPKCECodeVerifierMissingError'
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Error thrown when a transient fetch issue occurs.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* import { AuthRetryableFetchError } from '@supabase/auth-js'
|
|
264
|
+
*
|
|
265
|
+
* throw new AuthRetryableFetchError('Service temporarily unavailable', 503)
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
export class AuthRetryableFetchError extends CustomAuthError {
|
|
269
|
+
constructor(message: string, status: number) {
|
|
270
|
+
super(message, 'AuthRetryableFetchError', status, undefined)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function isAuthRetryableFetchError(error: unknown): error is AuthRetryableFetchError {
|
|
275
|
+
return isAuthError(error) && error.name === 'AuthRetryableFetchError'
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* This error is thrown on certain methods when the password used is deemed
|
|
280
|
+
* weak. Inspect the reasons to identify what password strength rules are
|
|
281
|
+
* inadequate.
|
|
282
|
+
*/
|
|
283
|
+
/**
|
|
284
|
+
* Error thrown when a supplied password is considered weak.
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```ts
|
|
288
|
+
* import { AuthWeakPasswordError } from '@supabase/auth-js'
|
|
289
|
+
*
|
|
290
|
+
* throw new AuthWeakPasswordError('Password too short', 400, ['min_length'])
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
export class AuthWeakPasswordError extends CustomAuthError {
|
|
294
|
+
/**
|
|
295
|
+
* Reasons why the password is deemed weak.
|
|
296
|
+
*/
|
|
297
|
+
reasons: WeakPasswordReasons[]
|
|
298
|
+
|
|
299
|
+
constructor(message: string, status: number, reasons: WeakPasswordReasons[]) {
|
|
300
|
+
super(message, 'AuthWeakPasswordError', status, 'weak_password')
|
|
301
|
+
|
|
302
|
+
this.reasons = reasons
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function isAuthWeakPasswordError(error: unknown): error is AuthWeakPasswordError {
|
|
307
|
+
return isAuthError(error) && error.name === 'AuthWeakPasswordError'
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Error thrown when a JWT cannot be verified or parsed.
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```ts
|
|
315
|
+
* import { AuthInvalidJwtError } from '@supabase/auth-js'
|
|
316
|
+
*
|
|
317
|
+
* throw new AuthInvalidJwtError('Token signature is invalid')
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
export class AuthInvalidJwtError extends CustomAuthError {
|
|
321
|
+
constructor(message: string) {
|
|
322
|
+
super(message, 'AuthInvalidJwtError', 400, 'invalid_jwt')
|
|
323
|
+
}
|
|
324
|
+
}
|
package/src/lib/fetch.ts
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { API_VERSIONS, API_VERSION_HEADER_NAME } from './constants'
|
|
2
|
+
import { expiresAt, looksLikeFetchResponse, parseResponseAPIVersion } from './helpers'
|
|
3
|
+
import {
|
|
4
|
+
AuthResponse,
|
|
5
|
+
AuthResponsePassword,
|
|
6
|
+
SSOResponse,
|
|
7
|
+
GenerateLinkProperties,
|
|
8
|
+
GenerateLinkResponse,
|
|
9
|
+
User,
|
|
10
|
+
UserResponse,
|
|
11
|
+
} from './types'
|
|
12
|
+
import {
|
|
13
|
+
AuthApiError,
|
|
14
|
+
AuthRetryableFetchError,
|
|
15
|
+
AuthWeakPasswordError,
|
|
16
|
+
AuthUnknownError,
|
|
17
|
+
AuthSessionMissingError,
|
|
18
|
+
} from './errors'
|
|
19
|
+
|
|
20
|
+
export type Fetch = typeof fetch
|
|
21
|
+
|
|
22
|
+
export interface FetchOptions {
|
|
23
|
+
headers?: {
|
|
24
|
+
[key: string]: string
|
|
25
|
+
}
|
|
26
|
+
noResolveJson?: boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface FetchParameters {
|
|
30
|
+
signal?: AbortSignal
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE'
|
|
34
|
+
|
|
35
|
+
const _getErrorMessage = (err: any): string =>
|
|
36
|
+
err.msg || err.message || err.error_description || err.error || JSON.stringify(err)
|
|
37
|
+
|
|
38
|
+
const NETWORK_ERROR_CODES = [502, 503, 504]
|
|
39
|
+
|
|
40
|
+
export async function handleError(error: unknown) {
|
|
41
|
+
if (!looksLikeFetchResponse(error)) {
|
|
42
|
+
throw new AuthRetryableFetchError(_getErrorMessage(error), 0)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (NETWORK_ERROR_CODES.includes(error.status)) {
|
|
46
|
+
// status in 500...599 range - server had an error, request might be retryed.
|
|
47
|
+
throw new AuthRetryableFetchError(_getErrorMessage(error), error.status)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let data: any
|
|
51
|
+
try {
|
|
52
|
+
data = await error.json()
|
|
53
|
+
} catch (e: any) {
|
|
54
|
+
throw new AuthUnknownError(_getErrorMessage(e), e)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let errorCode: string | undefined = undefined
|
|
58
|
+
|
|
59
|
+
const responseAPIVersion = parseResponseAPIVersion(error)
|
|
60
|
+
if (
|
|
61
|
+
responseAPIVersion &&
|
|
62
|
+
responseAPIVersion.getTime() >= API_VERSIONS['2024-01-01'].timestamp &&
|
|
63
|
+
typeof data === 'object' &&
|
|
64
|
+
data &&
|
|
65
|
+
typeof data.code === 'string'
|
|
66
|
+
) {
|
|
67
|
+
errorCode = data.code
|
|
68
|
+
} else if (typeof data === 'object' && data && typeof data.error_code === 'string') {
|
|
69
|
+
errorCode = data.error_code
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!errorCode) {
|
|
73
|
+
// Legacy support for weak password errors, when there were no error codes
|
|
74
|
+
if (
|
|
75
|
+
typeof data === 'object' &&
|
|
76
|
+
data &&
|
|
77
|
+
typeof data.weak_password === 'object' &&
|
|
78
|
+
data.weak_password &&
|
|
79
|
+
Array.isArray(data.weak_password.reasons) &&
|
|
80
|
+
data.weak_password.reasons.length &&
|
|
81
|
+
data.weak_password.reasons.reduce((a: boolean, i: any) => a && typeof i === 'string', true)
|
|
82
|
+
) {
|
|
83
|
+
throw new AuthWeakPasswordError(
|
|
84
|
+
_getErrorMessage(data),
|
|
85
|
+
error.status,
|
|
86
|
+
data.weak_password.reasons
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
} else if (errorCode === 'weak_password') {
|
|
90
|
+
throw new AuthWeakPasswordError(
|
|
91
|
+
_getErrorMessage(data),
|
|
92
|
+
error.status,
|
|
93
|
+
data.weak_password?.reasons || []
|
|
94
|
+
)
|
|
95
|
+
} else if (errorCode === 'session_not_found') {
|
|
96
|
+
// The `session_id` inside the JWT does not correspond to a row in the
|
|
97
|
+
// `sessions` table. This usually means the user has signed out, has been
|
|
98
|
+
// deleted, or their session has somehow been terminated.
|
|
99
|
+
throw new AuthSessionMissingError()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
throw new AuthApiError(_getErrorMessage(data), error.status || 500, errorCode)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const _getRequestParams = (
|
|
106
|
+
method: RequestMethodType,
|
|
107
|
+
options?: FetchOptions,
|
|
108
|
+
parameters?: FetchParameters,
|
|
109
|
+
body?: object
|
|
110
|
+
) => {
|
|
111
|
+
const params: { [k: string]: any } = { method, headers: options?.headers || {} }
|
|
112
|
+
|
|
113
|
+
if (method === 'GET') {
|
|
114
|
+
return params
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
params.headers = { 'Content-Type': 'application/json;charset=UTF-8', ...options?.headers }
|
|
118
|
+
params.body = JSON.stringify(body)
|
|
119
|
+
return { ...params, ...parameters }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface GotrueRequestOptions extends FetchOptions {
|
|
123
|
+
jwt?: string
|
|
124
|
+
redirectTo?: string
|
|
125
|
+
body?: object
|
|
126
|
+
query?: { [key: string]: string }
|
|
127
|
+
/**
|
|
128
|
+
* Function that transforms api response from gotrue into a desirable / standardised format
|
|
129
|
+
*/
|
|
130
|
+
xform?: (data: any) => any
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function _request(
|
|
134
|
+
fetcher: Fetch,
|
|
135
|
+
method: RequestMethodType,
|
|
136
|
+
url: string,
|
|
137
|
+
options?: GotrueRequestOptions
|
|
138
|
+
) {
|
|
139
|
+
const headers = {
|
|
140
|
+
...options?.headers,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!headers[API_VERSION_HEADER_NAME]) {
|
|
144
|
+
headers[API_VERSION_HEADER_NAME] = API_VERSIONS['2024-01-01'].name
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (options?.jwt) {
|
|
148
|
+
headers['Authorization'] = `Bearer ${options.jwt}`
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const qs = options?.query ?? {}
|
|
152
|
+
if (options?.redirectTo) {
|
|
153
|
+
qs['redirect_to'] = options.redirectTo
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const queryString = Object.keys(qs).length ? '?' + new URLSearchParams(qs).toString() : ''
|
|
157
|
+
const data = await _handleRequest(
|
|
158
|
+
fetcher,
|
|
159
|
+
method,
|
|
160
|
+
url + queryString,
|
|
161
|
+
{
|
|
162
|
+
headers,
|
|
163
|
+
noResolveJson: options?.noResolveJson,
|
|
164
|
+
},
|
|
165
|
+
{},
|
|
166
|
+
options?.body
|
|
167
|
+
)
|
|
168
|
+
return options?.xform ? options?.xform(data) : { data: { ...data }, error: null }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function _handleRequest(
|
|
172
|
+
fetcher: Fetch,
|
|
173
|
+
method: RequestMethodType,
|
|
174
|
+
url: string,
|
|
175
|
+
options?: FetchOptions,
|
|
176
|
+
parameters?: FetchParameters,
|
|
177
|
+
body?: object
|
|
178
|
+
): Promise<any> {
|
|
179
|
+
const requestParams = _getRequestParams(method, options, parameters, body)
|
|
180
|
+
|
|
181
|
+
let result: any
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
result = await fetcher(url, {
|
|
185
|
+
...requestParams,
|
|
186
|
+
})
|
|
187
|
+
} catch (e) {
|
|
188
|
+
console.error(e)
|
|
189
|
+
|
|
190
|
+
// fetch failed, likely due to a network or CORS error
|
|
191
|
+
throw new AuthRetryableFetchError(_getErrorMessage(e), 0)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (!result.ok) {
|
|
195
|
+
await handleError(result)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (options?.noResolveJson) {
|
|
199
|
+
return result
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
return await result.json()
|
|
204
|
+
} catch (e: any) {
|
|
205
|
+
await handleError(e)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function _sessionResponse(data: any): AuthResponse {
|
|
210
|
+
let session = null
|
|
211
|
+
if (hasSession(data)) {
|
|
212
|
+
session = { ...data }
|
|
213
|
+
|
|
214
|
+
if (!data.expires_at) {
|
|
215
|
+
session.expires_at = expiresAt(data.expires_in)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const user: User = data.user ?? (data as User)
|
|
220
|
+
return { data: { session, user }, error: null }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function _sessionResponsePassword(data: any): AuthResponsePassword {
|
|
224
|
+
const response = _sessionResponse(data) as AuthResponsePassword
|
|
225
|
+
|
|
226
|
+
if (
|
|
227
|
+
!response.error &&
|
|
228
|
+
data.weak_password &&
|
|
229
|
+
typeof data.weak_password === 'object' &&
|
|
230
|
+
Array.isArray(data.weak_password.reasons) &&
|
|
231
|
+
data.weak_password.reasons.length &&
|
|
232
|
+
data.weak_password.message &&
|
|
233
|
+
typeof data.weak_password.message === 'string' &&
|
|
234
|
+
data.weak_password.reasons.reduce((a: boolean, i: any) => a && typeof i === 'string', true)
|
|
235
|
+
) {
|
|
236
|
+
response.data.weak_password = data.weak_password
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return response
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function _userResponse(data: any): UserResponse {
|
|
243
|
+
const user: User = data.user ?? (data as User)
|
|
244
|
+
return { data: { user }, error: null }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export function _ssoResponse(data: any): SSOResponse {
|
|
248
|
+
return { data, error: null }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function _generateLinkResponse(data: any): GenerateLinkResponse {
|
|
252
|
+
const { action_link, email_otp, hashed_token, redirect_to, verification_type, ...rest } = data
|
|
253
|
+
|
|
254
|
+
const properties: GenerateLinkProperties = {
|
|
255
|
+
action_link,
|
|
256
|
+
email_otp,
|
|
257
|
+
hashed_token,
|
|
258
|
+
redirect_to,
|
|
259
|
+
verification_type,
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const user: User = { ...rest }
|
|
263
|
+
return {
|
|
264
|
+
data: {
|
|
265
|
+
properties,
|
|
266
|
+
user,
|
|
267
|
+
},
|
|
268
|
+
error: null,
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function _noResolveJsonResponse(data: any): Response {
|
|
273
|
+
return data
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* hasSession checks if the response object contains a valid session
|
|
278
|
+
* @param data A response object
|
|
279
|
+
* @returns true if a session is in the response
|
|
280
|
+
*/
|
|
281
|
+
function hasSession(data: any): boolean {
|
|
282
|
+
return data.access_token && data.refresh_token && data.expires_in
|
|
283
|
+
}
|