@supabase/gotrue-js 1.21.7 → 1.22.2
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/dist/main/GoTrueApi.d.ts +148 -76
- package/dist/main/GoTrueApi.d.ts.map +1 -1
- package/dist/main/GoTrueApi.js +337 -148
- package/dist/main/GoTrueApi.js.map +1 -1
- package/dist/main/GoTrueClient.d.ts +6 -1
- package/dist/main/GoTrueClient.d.ts.map +1 -1
- package/dist/main/GoTrueClient.js +38 -3
- package/dist/main/GoTrueClient.js.map +1 -1
- package/dist/main/lib/constants.js +1 -1
- package/dist/main/lib/constants.js.map +1 -1
- package/dist/main/lib/cookies.d.ts +4 -0
- package/dist/main/lib/cookies.d.ts.map +1 -1
- package/dist/main/lib/cookies.js +11 -4
- package/dist/main/lib/cookies.js.map +1 -1
- package/dist/main/lib/types.d.ts +45 -2
- package/dist/main/lib/types.d.ts.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/module/GoTrueApi.d.ts +148 -76
- package/dist/module/GoTrueApi.d.ts.map +1 -1
- package/dist/module/GoTrueApi.js +338 -149
- package/dist/module/GoTrueApi.js.map +1 -1
- package/dist/module/GoTrueClient.d.ts +6 -1
- package/dist/module/GoTrueClient.d.ts.map +1 -1
- package/dist/module/GoTrueClient.js +38 -3
- package/dist/module/GoTrueClient.js.map +1 -1
- package/dist/module/lib/constants.js +1 -1
- package/dist/module/lib/constants.js.map +1 -1
- package/dist/module/lib/cookies.d.ts +4 -0
- package/dist/module/lib/cookies.d.ts.map +1 -1
- package/dist/module/lib/cookies.js +9 -3
- package/dist/module/lib/cookies.js.map +1 -1
- package/dist/module/lib/types.d.ts +45 -2
- package/dist/module/lib/types.d.ts.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/package.json +7 -5
- package/src/GoTrueApi.ts +426 -178
- package/src/GoTrueClient.ts +51 -3
- package/src/lib/constants.ts +1 -1
- package/src/lib/cookies.ts +10 -3
- package/src/lib/types.ts +59 -7
- package/src/lib/version.ts +1 -1
package/dist/module/GoTrueApi.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { get, post, put, remove } from './lib/fetch';
|
|
11
11
|
import { COOKIE_OPTIONS } from './lib/constants';
|
|
12
|
-
import {
|
|
12
|
+
import { setCookies, getCookieString } from './lib/cookies';
|
|
13
13
|
import { expiresAt } from './lib/helpers';
|
|
14
14
|
export default class GoTrueApi {
|
|
15
15
|
constructor({ url = '', headers = {}, cookieOptions, fetch, }) {
|
|
@@ -19,43 +19,34 @@ export default class GoTrueApi {
|
|
|
19
19
|
this.fetch = fetch;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @param attributes The data you want to create the user with.
|
|
27
|
-
* @param jwt A valid JWT. Must be a full-access API key (e.g. service_role key).
|
|
22
|
+
* Create a temporary object with all configured headers and
|
|
23
|
+
* adds the Authorization token to be used on request methods
|
|
24
|
+
* @param jwt A valid, logged-in JWT.
|
|
28
25
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
catch (e) {
|
|
38
|
-
return { data: null, error: e };
|
|
39
|
-
}
|
|
40
|
-
});
|
|
26
|
+
_createRequestHeaders(jwt) {
|
|
27
|
+
const headers = Object.assign({}, this.headers);
|
|
28
|
+
headers['Authorization'] = `Bearer ${jwt}`;
|
|
29
|
+
return headers;
|
|
30
|
+
}
|
|
31
|
+
cookieName() {
|
|
32
|
+
var _a;
|
|
33
|
+
return (_a = this.cookieOptions.name) !== null && _a !== void 0 ? _a : '';
|
|
41
34
|
}
|
|
42
35
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
36
|
+
* Generates the relevant login URL for a third-party provider.
|
|
37
|
+
* @param provider One of the providers supported by GoTrue.
|
|
38
|
+
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
|
|
39
|
+
* @param scopes A space-separated list of scopes granted to the OAuth application.
|
|
46
40
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return { data: null, error: e };
|
|
57
|
-
}
|
|
58
|
-
});
|
|
41
|
+
getUrlForProvider(provider, options) {
|
|
42
|
+
const urlParams = [`provider=${encodeURIComponent(provider)}`];
|
|
43
|
+
if (options === null || options === void 0 ? void 0 : options.redirectTo) {
|
|
44
|
+
urlParams.push(`redirect_to=${encodeURIComponent(options.redirectTo)}`);
|
|
45
|
+
}
|
|
46
|
+
if (options === null || options === void 0 ? void 0 : options.scopes) {
|
|
47
|
+
urlParams.push(`scopes=${encodeURIComponent(options.scopes)}`);
|
|
48
|
+
}
|
|
49
|
+
return `${this.url}/authorize?${urlParams.join('&')}`;
|
|
59
50
|
}
|
|
60
51
|
/**
|
|
61
52
|
* Creates a new user using their email address.
|
|
@@ -75,7 +66,12 @@ export default class GoTrueApi {
|
|
|
75
66
|
if (options.redirectTo) {
|
|
76
67
|
queryString = '?redirect_to=' + encodeURIComponent(options.redirectTo);
|
|
77
68
|
}
|
|
78
|
-
const data = yield post(this.fetch, `${this.url}/signup${queryString}`, {
|
|
69
|
+
const data = yield post(this.fetch, `${this.url}/signup${queryString}`, {
|
|
70
|
+
email,
|
|
71
|
+
password,
|
|
72
|
+
data: options.data,
|
|
73
|
+
gotrue_meta_security: { hcaptcha_token: options.captchaToken },
|
|
74
|
+
}, { headers });
|
|
79
75
|
const session = Object.assign({}, data);
|
|
80
76
|
if (session.expires_in)
|
|
81
77
|
session.expires_at = expiresAt(data.expires_in);
|
|
@@ -121,7 +117,12 @@ export default class GoTrueApi {
|
|
|
121
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
122
118
|
try {
|
|
123
119
|
const headers = Object.assign({}, this.headers);
|
|
124
|
-
const data = yield post(this.fetch, `${this.url}/signup`, {
|
|
120
|
+
const data = yield post(this.fetch, `${this.url}/signup`, {
|
|
121
|
+
phone,
|
|
122
|
+
password,
|
|
123
|
+
data: options.data,
|
|
124
|
+
gotrue_meta_security: { hcaptcha_token: options.captchaToken },
|
|
125
|
+
}, { headers });
|
|
125
126
|
const session = Object.assign({}, data);
|
|
126
127
|
if (session.expires_in)
|
|
127
128
|
session.expires_at = expiresAt(data.expires_in);
|
|
@@ -153,9 +154,34 @@ export default class GoTrueApi {
|
|
|
153
154
|
}
|
|
154
155
|
});
|
|
155
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Logs in an OpenID Connect user using their id_token.
|
|
159
|
+
* @param id_token The IDToken of the user.
|
|
160
|
+
* @param nonce The nonce of the user. The nonce is a random value generated by the developer (= yourself) before the initial grant is started. You should check the OpenID Connect specification for details. https://openid.net/developers/specs/
|
|
161
|
+
* @param provider The provider of the user.
|
|
162
|
+
* @param client_id The clientID of the user.
|
|
163
|
+
* @param issuer The issuer of the user.
|
|
164
|
+
*/
|
|
165
|
+
signInWithOpenIDConnect({ id_token, nonce, client_id, issuer, provider, }) {
|
|
166
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
167
|
+
try {
|
|
168
|
+
const headers = Object.assign({}, this.headers);
|
|
169
|
+
const queryString = '?grant_type=id_token';
|
|
170
|
+
const data = yield post(this.fetch, `${this.url}/token${queryString}`, { id_token, nonce, client_id, issuer, provider }, { headers });
|
|
171
|
+
const session = Object.assign({}, data);
|
|
172
|
+
if (session.expires_in)
|
|
173
|
+
session.expires_at = expiresAt(data.expires_in);
|
|
174
|
+
return { data: session, error: null };
|
|
175
|
+
}
|
|
176
|
+
catch (e) {
|
|
177
|
+
return { data: null, error: e };
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
156
181
|
/**
|
|
157
182
|
* Sends a magic login link to an email address.
|
|
158
183
|
* @param email The email address of the user.
|
|
184
|
+
* @param shouldCreateUser A boolean flag to indicate whether to automatically create a user on magiclink / otp sign-ins if the user doesn't exist. Defaults to true.
|
|
159
185
|
* @param redirectTo A URL or mobile address to send the user to after they are confirmed.
|
|
160
186
|
*/
|
|
161
187
|
sendMagicLinkEmail(email, options = {}) {
|
|
@@ -166,7 +192,12 @@ export default class GoTrueApi {
|
|
|
166
192
|
if (options.redirectTo) {
|
|
167
193
|
queryString += '?redirect_to=' + encodeURIComponent(options.redirectTo);
|
|
168
194
|
}
|
|
169
|
-
const
|
|
195
|
+
const shouldCreateUser = options.shouldCreateUser ? options.shouldCreateUser : true;
|
|
196
|
+
const data = yield post(this.fetch, `${this.url}/otp${queryString}`, {
|
|
197
|
+
email,
|
|
198
|
+
create_user: shouldCreateUser,
|
|
199
|
+
gotrue_meta_security: { hcaptcha_token: options.captchaToken },
|
|
200
|
+
}, { headers });
|
|
170
201
|
return { data, error: null };
|
|
171
202
|
}
|
|
172
203
|
catch (e) {
|
|
@@ -177,12 +208,18 @@ export default class GoTrueApi {
|
|
|
177
208
|
/**
|
|
178
209
|
* Sends a mobile OTP via SMS. Will register the account if it doesn't already exist
|
|
179
210
|
* @param phone The user's phone number WITH international prefix
|
|
211
|
+
* @param shouldCreateUser A boolean flag to indicate whether to automatically create a user on magiclink / otp sign-ins if the user doesn't exist. Defaults to true.
|
|
180
212
|
*/
|
|
181
|
-
sendMobileOTP(phone) {
|
|
213
|
+
sendMobileOTP(phone, options = {}) {
|
|
182
214
|
return __awaiter(this, void 0, void 0, function* () {
|
|
183
215
|
try {
|
|
216
|
+
const shouldCreateUser = options.shouldCreateUser ? options.shouldCreateUser : true;
|
|
184
217
|
const headers = Object.assign({}, this.headers);
|
|
185
|
-
const data = yield post(this.fetch, `${this.url}/otp`, {
|
|
218
|
+
const data = yield post(this.fetch, `${this.url}/otp`, {
|
|
219
|
+
phone,
|
|
220
|
+
create_user: shouldCreateUser,
|
|
221
|
+
gotrue_meta_security: { hcaptcha_token: options.captchaToken },
|
|
222
|
+
}, { headers });
|
|
186
223
|
return { data, error: null };
|
|
187
224
|
}
|
|
188
225
|
catch (e) {
|
|
@@ -190,6 +227,21 @@ export default class GoTrueApi {
|
|
|
190
227
|
}
|
|
191
228
|
});
|
|
192
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Removes a logged-in session.
|
|
232
|
+
* @param jwt A valid, logged-in JWT.
|
|
233
|
+
*/
|
|
234
|
+
signOut(jwt) {
|
|
235
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
236
|
+
try {
|
|
237
|
+
yield post(this.fetch, `${this.url}/logout`, {}, { headers: this._createRequestHeaders(jwt), noResolveJson: true });
|
|
238
|
+
return { error: null };
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
return { error: e };
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
193
245
|
/**
|
|
194
246
|
* Send User supplied Mobile OTP to be verified
|
|
195
247
|
* @param phone The user's phone number WITH international prefix
|
|
@@ -243,7 +295,7 @@ export default class GoTrueApi {
|
|
|
243
295
|
if (options.redirectTo) {
|
|
244
296
|
queryString += '?redirect_to=' + encodeURIComponent(options.redirectTo);
|
|
245
297
|
}
|
|
246
|
-
const data = yield post(this.fetch, `${this.url}/recover${queryString}`, { email }, { headers });
|
|
298
|
+
const data = yield post(this.fetch, `${this.url}/recover${queryString}`, { email, gotrue_meta_security: { hcaptcha_token: options.captchaToken } }, { headers });
|
|
247
299
|
return { data, error: null };
|
|
248
300
|
}
|
|
249
301
|
catch (e) {
|
|
@@ -252,73 +304,157 @@ export default class GoTrueApi {
|
|
|
252
304
|
});
|
|
253
305
|
}
|
|
254
306
|
/**
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* @param jwt A valid, logged-in JWT.
|
|
258
|
-
*/
|
|
259
|
-
_createRequestHeaders(jwt) {
|
|
260
|
-
const headers = Object.assign({}, this.headers);
|
|
261
|
-
headers['Authorization'] = `Bearer ${jwt}`;
|
|
262
|
-
return headers;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Removes a logged-in session.
|
|
266
|
-
* @param jwt A valid, logged-in JWT.
|
|
307
|
+
* Generates a new JWT.
|
|
308
|
+
* @param refreshToken A valid refresh token that was returned on login.
|
|
267
309
|
*/
|
|
268
|
-
|
|
310
|
+
refreshAccessToken(refreshToken) {
|
|
269
311
|
return __awaiter(this, void 0, void 0, function* () {
|
|
270
312
|
try {
|
|
271
|
-
yield post(this.fetch, `${this.url}/
|
|
272
|
-
|
|
313
|
+
const data = yield post(this.fetch, `${this.url}/token?grant_type=refresh_token`, { refresh_token: refreshToken }, { headers: this.headers });
|
|
314
|
+
const session = Object.assign({}, data);
|
|
315
|
+
if (session.expires_in)
|
|
316
|
+
session.expires_at = expiresAt(data.expires_in);
|
|
317
|
+
return { data: session, error: null };
|
|
273
318
|
}
|
|
274
319
|
catch (e) {
|
|
275
|
-
return { error: e };
|
|
320
|
+
return { data: null, error: e };
|
|
276
321
|
}
|
|
277
322
|
});
|
|
278
323
|
}
|
|
279
324
|
/**
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* @param
|
|
283
|
-
* @param
|
|
325
|
+
* Set/delete the auth cookie based on the AuthChangeEvent.
|
|
326
|
+
* Works for Next.js & Express (requires cookie-parser middleware).
|
|
327
|
+
* @param req The request object.
|
|
328
|
+
* @param res The response object.
|
|
284
329
|
*/
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
330
|
+
setAuthCookie(req, res) {
|
|
331
|
+
if (req.method !== 'POST') {
|
|
332
|
+
res.setHeader('Allow', 'POST');
|
|
333
|
+
res.status(405).end('Method Not Allowed');
|
|
289
334
|
}
|
|
290
|
-
|
|
291
|
-
|
|
335
|
+
const { event, session } = req.body;
|
|
336
|
+
if (!event)
|
|
337
|
+
throw new Error('Auth event missing!');
|
|
338
|
+
if (event === 'SIGNED_IN') {
|
|
339
|
+
if (!session)
|
|
340
|
+
throw new Error('Auth session missing!');
|
|
341
|
+
setCookies(req, res, [
|
|
342
|
+
{ key: 'access-token', value: session.access_token },
|
|
343
|
+
{ key: 'refresh-token', value: session.refresh_token },
|
|
344
|
+
].map((token) => {
|
|
345
|
+
var _a;
|
|
346
|
+
return ({
|
|
347
|
+
name: `${this.cookieName()}-${token.key}`,
|
|
348
|
+
value: token.value,
|
|
349
|
+
domain: this.cookieOptions.domain,
|
|
350
|
+
maxAge: (_a = this.cookieOptions.lifetime) !== null && _a !== void 0 ? _a : 0,
|
|
351
|
+
path: this.cookieOptions.path,
|
|
352
|
+
sameSite: this.cookieOptions.sameSite,
|
|
353
|
+
});
|
|
354
|
+
}));
|
|
292
355
|
}
|
|
293
|
-
|
|
356
|
+
if (event === 'SIGNED_OUT') {
|
|
357
|
+
setCookies(req, res, ['access-token', 'refresh-token'].map((key) => ({
|
|
358
|
+
name: `${this.cookieName()}-${key}`,
|
|
359
|
+
value: '',
|
|
360
|
+
maxAge: -1,
|
|
361
|
+
})));
|
|
362
|
+
}
|
|
363
|
+
res.status(200).json({});
|
|
294
364
|
}
|
|
295
365
|
/**
|
|
296
|
-
*
|
|
297
|
-
* @param
|
|
366
|
+
* Deletes the Auth Cookies and redirects to the
|
|
367
|
+
* @param req The request object.
|
|
368
|
+
* @param res The response object.
|
|
369
|
+
* @param options Optionally specify a `redirectTo` URL in the options.
|
|
298
370
|
*/
|
|
299
|
-
|
|
371
|
+
deleteAuthCookie(req, res, { redirectTo = '/' }) {
|
|
372
|
+
setCookies(req, res, ['access-token', 'refresh-token'].map((key) => ({
|
|
373
|
+
name: `${this.cookieName()}-${key}`,
|
|
374
|
+
value: '',
|
|
375
|
+
maxAge: -1,
|
|
376
|
+
})));
|
|
377
|
+
return res.redirect(307, redirectTo);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Helper method to generate the Auth Cookie string for you in case you can't use `setAuthCookie`.
|
|
381
|
+
* @param req The request object.
|
|
382
|
+
* @param res The response object.
|
|
383
|
+
* @returns The Cookie string that needs to be set as the value for the `Set-Cookie` header.
|
|
384
|
+
*/
|
|
385
|
+
getAuthCookieString(req, res) {
|
|
386
|
+
if (req.method !== 'POST') {
|
|
387
|
+
res.setHeader('Allow', 'POST');
|
|
388
|
+
res.status(405).end('Method Not Allowed');
|
|
389
|
+
}
|
|
390
|
+
const { event, session } = req.body;
|
|
391
|
+
if (!event)
|
|
392
|
+
throw new Error('Auth event missing!');
|
|
393
|
+
if (event === 'SIGNED_IN') {
|
|
394
|
+
if (!session)
|
|
395
|
+
throw new Error('Auth session missing!');
|
|
396
|
+
return getCookieString(req, res, [
|
|
397
|
+
{ key: 'access-token', value: session.access_token },
|
|
398
|
+
{ key: 'refresh-token', value: session.refresh_token },
|
|
399
|
+
].map((token) => {
|
|
400
|
+
var _a;
|
|
401
|
+
return ({
|
|
402
|
+
name: `${this.cookieName()}-${token.key}`,
|
|
403
|
+
value: token.value,
|
|
404
|
+
domain: this.cookieOptions.domain,
|
|
405
|
+
maxAge: (_a = this.cookieOptions.lifetime) !== null && _a !== void 0 ? _a : 0,
|
|
406
|
+
path: this.cookieOptions.path,
|
|
407
|
+
sameSite: this.cookieOptions.sameSite,
|
|
408
|
+
});
|
|
409
|
+
}));
|
|
410
|
+
}
|
|
411
|
+
if (event === 'SIGNED_OUT') {
|
|
412
|
+
return getCookieString(req, res, ['access-token', 'refresh-token'].map((key) => ({
|
|
413
|
+
name: `${this.cookieName()}-${key}`,
|
|
414
|
+
value: '',
|
|
415
|
+
maxAge: -1,
|
|
416
|
+
})));
|
|
417
|
+
}
|
|
418
|
+
return res.getHeader('Set-Cookie');
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Generates links to be sent via email or other.
|
|
422
|
+
* @param type The link type ("signup" or "magiclink" or "recovery" or "invite").
|
|
423
|
+
* @param email The user's email.
|
|
424
|
+
* @param password User password. For signup only.
|
|
425
|
+
* @param data Optional user metadata. For signup only.
|
|
426
|
+
* @param redirectTo The link type ("signup" or "magiclink" or "recovery" or "invite").
|
|
427
|
+
*/
|
|
428
|
+
generateLink(type, email, options = {}) {
|
|
300
429
|
return __awaiter(this, void 0, void 0, function* () {
|
|
301
430
|
try {
|
|
302
|
-
const data = yield
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
431
|
+
const data = yield post(this.fetch, `${this.url}/admin/generate_link`, {
|
|
432
|
+
type,
|
|
433
|
+
email,
|
|
434
|
+
password: options.password,
|
|
435
|
+
data: options.data,
|
|
436
|
+
redirect_to: options.redirectTo,
|
|
437
|
+
}, { headers: this.headers });
|
|
438
|
+
return { data, error: null };
|
|
306
439
|
}
|
|
307
440
|
catch (e) {
|
|
308
|
-
return {
|
|
441
|
+
return { data: null, error: e };
|
|
309
442
|
}
|
|
310
443
|
});
|
|
311
444
|
}
|
|
445
|
+
// User Admin API
|
|
312
446
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
447
|
+
* Creates a new user.
|
|
448
|
+
*
|
|
449
|
+
* This function should only be called on a server. Never expose your `service_role` key in the browser.
|
|
450
|
+
*
|
|
451
|
+
* @param attributes The data you want to create the user with.
|
|
316
452
|
*/
|
|
317
|
-
|
|
453
|
+
createUser(attributes) {
|
|
318
454
|
return __awaiter(this, void 0, void 0, function* () {
|
|
319
455
|
try {
|
|
320
|
-
const data = yield
|
|
321
|
-
headers: this.
|
|
456
|
+
const data = yield post(this.fetch, `${this.url}/admin/users`, attributes, {
|
|
457
|
+
headers: this.headers,
|
|
322
458
|
});
|
|
323
459
|
return { user: data, data, error: null };
|
|
324
460
|
}
|
|
@@ -328,90 +464,87 @@ export default class GoTrueApi {
|
|
|
328
464
|
});
|
|
329
465
|
}
|
|
330
466
|
/**
|
|
331
|
-
*
|
|
467
|
+
* Get a list of users.
|
|
332
468
|
*
|
|
333
469
|
* This function should only be called on a server. Never expose your `service_role` key in the browser.
|
|
334
|
-
*
|
|
335
|
-
* @param uid The user uid you want to remove.
|
|
336
|
-
* @param jwt A valid JWT. Must be a full-access API key (e.g. service_role key).
|
|
337
470
|
*/
|
|
338
|
-
|
|
471
|
+
listUsers() {
|
|
339
472
|
return __awaiter(this, void 0, void 0, function* () {
|
|
340
473
|
try {
|
|
341
|
-
const data = yield
|
|
342
|
-
headers: this.
|
|
474
|
+
const data = yield get(this.fetch, `${this.url}/admin/users`, {
|
|
475
|
+
headers: this.headers,
|
|
343
476
|
});
|
|
344
|
-
return {
|
|
477
|
+
return { data: data.users, error: null };
|
|
345
478
|
}
|
|
346
479
|
catch (e) {
|
|
347
|
-
return {
|
|
480
|
+
return { data: null, error: e };
|
|
348
481
|
}
|
|
349
482
|
});
|
|
350
483
|
}
|
|
351
484
|
/**
|
|
352
|
-
*
|
|
353
|
-
*
|
|
485
|
+
* Get user by id.
|
|
486
|
+
*
|
|
487
|
+
* @param uid The user's unique identifier
|
|
488
|
+
*
|
|
489
|
+
* This function should only be called on a server. Never expose your `service_role` key in the browser.
|
|
354
490
|
*/
|
|
355
|
-
|
|
491
|
+
getUserById(uid) {
|
|
356
492
|
return __awaiter(this, void 0, void 0, function* () {
|
|
357
493
|
try {
|
|
358
|
-
const data = yield
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
return { data: session, error: null };
|
|
494
|
+
const data = yield get(this.fetch, `${this.url}/admin/users/${uid}`, {
|
|
495
|
+
headers: this.headers,
|
|
496
|
+
});
|
|
497
|
+
return { data, error: null };
|
|
363
498
|
}
|
|
364
499
|
catch (e) {
|
|
365
500
|
return { data: null, error: e };
|
|
366
501
|
}
|
|
367
502
|
});
|
|
368
503
|
}
|
|
369
|
-
/**
|
|
370
|
-
* Set/delete the auth cookie based on the AuthChangeEvent.
|
|
371
|
-
* Works for Next.js & Express (requires cookie-parser middleware).
|
|
372
|
-
*/
|
|
373
|
-
setAuthCookie(req, res) {
|
|
374
|
-
if (req.method !== 'POST') {
|
|
375
|
-
res.setHeader('Allow', 'POST');
|
|
376
|
-
res.status(405).end('Method Not Allowed');
|
|
377
|
-
}
|
|
378
|
-
const { event, session } = req.body;
|
|
379
|
-
if (!event)
|
|
380
|
-
throw new Error('Auth event missing!');
|
|
381
|
-
if (event === 'SIGNED_IN') {
|
|
382
|
-
if (!session)
|
|
383
|
-
throw new Error('Auth session missing!');
|
|
384
|
-
setCookie(req, res, {
|
|
385
|
-
name: this.cookieOptions.name,
|
|
386
|
-
value: session.access_token,
|
|
387
|
-
domain: this.cookieOptions.domain,
|
|
388
|
-
maxAge: this.cookieOptions.lifetime,
|
|
389
|
-
path: this.cookieOptions.path,
|
|
390
|
-
sameSite: this.cookieOptions.sameSite,
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
if (event === 'SIGNED_OUT')
|
|
394
|
-
deleteCookie(req, res, this.cookieOptions.name);
|
|
395
|
-
res.status(200).json({});
|
|
396
|
-
}
|
|
397
504
|
/**
|
|
398
505
|
* Get user by reading the cookie from the request.
|
|
399
506
|
* Works for Next.js & Express (requires cookie-parser middleware).
|
|
400
507
|
*/
|
|
401
|
-
getUserByCookie(req) {
|
|
508
|
+
getUserByCookie(req, res) {
|
|
402
509
|
return __awaiter(this, void 0, void 0, function* () {
|
|
403
510
|
try {
|
|
404
511
|
if (!req.cookies) {
|
|
405
512
|
throw new Error('Not able to parse cookies! When using Express make sure the cookie-parser middleware is in use!');
|
|
406
513
|
}
|
|
407
|
-
|
|
514
|
+
const access_token = req.cookies[`${this.cookieName()}-access-token`];
|
|
515
|
+
const refresh_token = req.cookies[`${this.cookieName()}-refresh-token`];
|
|
516
|
+
if (!access_token) {
|
|
408
517
|
throw new Error('No cookie found!');
|
|
409
518
|
}
|
|
410
|
-
const
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
519
|
+
const { user, error: getUserError } = yield this.getUser(access_token);
|
|
520
|
+
if (getUserError) {
|
|
521
|
+
if (!refresh_token)
|
|
522
|
+
throw new Error('No refresh_token cookie found!');
|
|
523
|
+
if (!res)
|
|
524
|
+
throw new Error('You need to pass the res object to automatically refresh the session!');
|
|
525
|
+
const { data, error } = yield this.refreshAccessToken(refresh_token);
|
|
526
|
+
if (error) {
|
|
527
|
+
throw error;
|
|
528
|
+
}
|
|
529
|
+
else if (data) {
|
|
530
|
+
setCookies(req, res, [
|
|
531
|
+
{ key: 'access-token', value: data.access_token },
|
|
532
|
+
{ key: 'refresh-token', value: data.refresh_token },
|
|
533
|
+
].map((token) => {
|
|
534
|
+
var _a;
|
|
535
|
+
return ({
|
|
536
|
+
name: `${this.cookieName()}-${token.key}`,
|
|
537
|
+
value: token.value,
|
|
538
|
+
domain: this.cookieOptions.domain,
|
|
539
|
+
maxAge: (_a = this.cookieOptions.lifetime) !== null && _a !== void 0 ? _a : 0,
|
|
540
|
+
path: this.cookieOptions.path,
|
|
541
|
+
sameSite: this.cookieOptions.sameSite,
|
|
542
|
+
});
|
|
543
|
+
}));
|
|
544
|
+
return { token: data.access_token, user: data.user, data: data.user, error: null };
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return { token: access_token, user: user, data: user, error: null };
|
|
415
548
|
}
|
|
416
549
|
catch (e) {
|
|
417
550
|
return { token: null, user: null, data: null, error: e };
|
|
@@ -419,27 +552,83 @@ export default class GoTrueApi {
|
|
|
419
552
|
});
|
|
420
553
|
}
|
|
421
554
|
/**
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
* @param
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
* @param redirectTo The link type ("signup" or "magiclink" or "recovery" or "invite").
|
|
555
|
+
* Updates the user data.
|
|
556
|
+
*
|
|
557
|
+
* @param attributes The data you want to update.
|
|
558
|
+
*
|
|
559
|
+
* This function should only be called on a server. Never expose your `service_role` key in the browser.
|
|
428
560
|
*/
|
|
429
|
-
|
|
561
|
+
updateUserById(uid, attributes) {
|
|
430
562
|
return __awaiter(this, void 0, void 0, function* () {
|
|
431
563
|
try {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
redirect_to: options.redirectTo,
|
|
438
|
-
}, { headers: this.headers });
|
|
439
|
-
return { data, error: null };
|
|
564
|
+
this; //
|
|
565
|
+
const data = yield put(this.fetch, `${this.url}/admin/users/${uid}`, attributes, {
|
|
566
|
+
headers: this.headers,
|
|
567
|
+
});
|
|
568
|
+
return { user: data, data, error: null };
|
|
440
569
|
}
|
|
441
570
|
catch (e) {
|
|
442
|
-
return { data: null, error: e };
|
|
571
|
+
return { user: null, data: null, error: e };
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Delete a user. Requires a `service_role` key.
|
|
577
|
+
*
|
|
578
|
+
* This function should only be called on a server. Never expose your `service_role` key in the browser.
|
|
579
|
+
*
|
|
580
|
+
* @param uid The user uid you want to remove.
|
|
581
|
+
*/
|
|
582
|
+
deleteUser(uid) {
|
|
583
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
584
|
+
try {
|
|
585
|
+
const data = yield remove(this.fetch, `${this.url}/admin/users/${uid}`, {}, {
|
|
586
|
+
headers: this.headers,
|
|
587
|
+
});
|
|
588
|
+
return { user: data, data, error: null };
|
|
589
|
+
}
|
|
590
|
+
catch (e) {
|
|
591
|
+
return { user: null, data: null, error: e };
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Gets the current user details.
|
|
597
|
+
*
|
|
598
|
+
* This method is called by the GoTrueClient `update` where
|
|
599
|
+
* the jwt is set to this.currentSession.access_token
|
|
600
|
+
* and therefore, acts like getting the currently authenticated used
|
|
601
|
+
*
|
|
602
|
+
* @param jwt A valid, logged-in JWT. Typically, the access_token for the currentSession
|
|
603
|
+
*/
|
|
604
|
+
getUser(jwt) {
|
|
605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
+
try {
|
|
607
|
+
const data = yield get(this.fetch, `${this.url}/user`, {
|
|
608
|
+
headers: this._createRequestHeaders(jwt),
|
|
609
|
+
});
|
|
610
|
+
return { user: data, data, error: null };
|
|
611
|
+
}
|
|
612
|
+
catch (e) {
|
|
613
|
+
return { user: null, data: null, error: e };
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Updates the user data.
|
|
619
|
+
* @param jwt A valid, logged-in JWT.
|
|
620
|
+
* @param attributes The data you want to update.
|
|
621
|
+
*/
|
|
622
|
+
updateUser(jwt, attributes) {
|
|
623
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
624
|
+
try {
|
|
625
|
+
const data = yield put(this.fetch, `${this.url}/user`, attributes, {
|
|
626
|
+
headers: this._createRequestHeaders(jwt),
|
|
627
|
+
});
|
|
628
|
+
return { user: data, data, error: null };
|
|
629
|
+
}
|
|
630
|
+
catch (e) {
|
|
631
|
+
return { user: null, data: null, error: e };
|
|
443
632
|
}
|
|
444
633
|
});
|
|
445
634
|
}
|