File size: 40,608 Bytes
c212805 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 | import { __rest } from "tslib";
import { _generateLinkResponse, _noResolveJsonResponse, _request, _userResponse, } from './lib/fetch';
import { assertPasskeyExperimentalEnabled, resolveFetch, validateUUID } from './lib/helpers';
import { SIGN_OUT_SCOPES, } from './lib/types';
import { isAuthError } from './lib/errors';
export default class GoTrueAdminApi {
/**
* Creates an admin API client that can be used to manage users and OAuth clients.
*
* @example Using supabase-js (recommended)
* ```ts
* import { createClient } from '@supabase/supabase-js'
*
* const supabase = createClient('https://xyzcompany.supabase.co', 'your-secret-key')
* const { data, error } = await supabase.auth.admin.listUsers()
* ```
*
* @example Standalone import for bundle-sensitive environments
* ```ts
* import { GoTrueAdminApi } from '@supabase/auth-js'
*
* const admin = new GoTrueAdminApi({
* url: 'https://xyzcompany.supabase.co/auth/v1',
* headers: { Authorization: `Bearer ${process.env.SUPABASE_SECRET_KEY}` },
* })
* ```
*/
constructor({ url = '', headers = {}, fetch, experimental, }) {
this.url = url;
this.headers = headers;
this.fetch = resolveFetch(fetch);
this.experimental = experimental !== null && experimental !== void 0 ? experimental : {};
this.mfa = {
listFactors: this._listFactors.bind(this),
deleteFactor: this._deleteFactor.bind(this),
};
this.oauth = {
listClients: this._listOAuthClients.bind(this),
createClient: this._createOAuthClient.bind(this),
getClient: this._getOAuthClient.bind(this),
updateClient: this._updateOAuthClient.bind(this),
deleteClient: this._deleteOAuthClient.bind(this),
regenerateClientSecret: this._regenerateOAuthClientSecret.bind(this),
};
this.customProviders = {
listProviders: this._listCustomProviders.bind(this),
createProvider: this._createCustomProvider.bind(this),
getProvider: this._getCustomProvider.bind(this),
updateProvider: this._updateCustomProvider.bind(this),
deleteProvider: this._deleteCustomProvider.bind(this),
};
this.passkey = {
listPasskeys: this._adminListPasskeys.bind(this),
deletePasskey: this._adminDeletePasskey.bind(this),
};
}
/**
* Removes a logged-in session.
* @param jwt A valid, logged-in JWT.
* @param scope The logout sope.
*
* @category Auth
* @subcategory Auth Admin
*/
async signOut(jwt, scope = SIGN_OUT_SCOPES[0]) {
if (SIGN_OUT_SCOPES.indexOf(scope) < 0) {
throw new Error(`@supabase/auth-js: Parameter scope must be one of ${SIGN_OUT_SCOPES.join(', ')}`);
}
try {
await _request(this.fetch, 'POST', `${this.url}/logout?scope=${scope}`, {
headers: this.headers,
jwt,
noResolveJson: true,
});
return { data: null, error: null };
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Sends an invite link to an email address.
* @param email The email address of the user.
* @param options Additional options to be included when inviting.
*
* @category Auth
* @subcategory Auth Admin
*
* @remarks
* - Sends an invite link to the user's email address.
* - The `inviteUserByEmail()` method is typically used by administrators to invite users to join the application.
* - Note that PKCE is not supported when using `inviteUserByEmail`. This is because the browser initiating the invite is often different from the browser accepting the invite which makes it difficult to provide the security guarantees required of the PKCE flow.
*
* @example Invite a user
* ```js
* const { data, error } = await supabase.auth.admin.inviteUserByEmail('email@example.com')
* ```
*
* @exampleResponse Invite a user
* ```json
* {
* "data": {
* "user": {
* "id": "11111111-1111-1111-1111-111111111111",
* "aud": "authenticated",
* "role": "authenticated",
* "email": "example@email.com",
* "invited_at": "2024-01-01T00:00:00Z",
* "phone": "",
* "confirmation_sent_at": "2024-01-01T00:00:00Z",
* "app_metadata": {
* "provider": "email",
* "providers": [
* "email"
* ]
* },
* "user_metadata": {},
* "identities": [
* {
* "identity_id": "22222222-2222-2222-2222-222222222222",
* "id": "11111111-1111-1111-1111-111111111111",
* "user_id": "11111111-1111-1111-1111-111111111111",
* "identity_data": {
* "email": "example@email.com",
* "email_verified": false,
* "phone_verified": false,
* "sub": "11111111-1111-1111-1111-111111111111"
* },
* "provider": "email",
* "last_sign_in_at": "2024-01-01T00:00:00Z",
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "email": "example@email.com"
* }
* ],
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "is_anonymous": false
* }
* },
* "error": null
* }
* ```
*/
async inviteUserByEmail(email, options = {}) {
try {
return await _request(this.fetch, 'POST', `${this.url}/invite`, {
body: { email, data: options.data },
headers: this.headers,
redirectTo: options.redirectTo,
xform: _userResponse,
});
}
catch (error) {
if (isAuthError(error)) {
return { data: { user: null }, error };
}
throw error;
}
}
/**
* Generates email links and OTPs to be sent via a custom email provider.
* @param params The parameters for generating the link, including the link `type`, the user's `email`, and type-specific options such as `password`, `data`, and `redirectTo`.
*
* @category Auth
* @subcategory Auth Admin
*
* @remarks
* - The following types can be passed into `generateLink()`: `signup`, `magiclink`, `invite`, `recovery`, `email_change_current`, `email_change_new`, `phone_change`.
* - `generateLink()` only generates the email link for `email_change_email` if the **Secure email change** is enabled in your project's [email auth provider settings](/dashboard/project/_/auth/providers).
* - `generateLink()` handles the creation of the user for `signup`, `invite` and `magiclink`.
*
* @example Generate a signup link
* ```js
* const { data, error } = await supabase.auth.admin.generateLink({
* type: 'signup',
* email: 'email@example.com',
* password: 'secret'
* })
* ```
*
* @exampleResponse Generate a signup link
* ```json
* {
* "data": {
* "properties": {
* "action_link": "<LINK_TO_SEND_TO_USER>",
* "email_otp": "999999",
* "hashed_token": "<HASHED_TOKEN",
* "redirect_to": "<REDIRECT_URL>",
* "verification_type": "signup"
* },
* "user": {
* "id": "11111111-1111-1111-1111-111111111111",
* "aud": "authenticated",
* "role": "authenticated",
* "email": "email@example.com",
* "phone": "",
* "confirmation_sent_at": "2024-01-01T00:00:00Z",
* "app_metadata": {
* "provider": "email",
* "providers": [
* "email"
* ]
* },
* "user_metadata": {},
* "identities": [
* {
* "identity_id": "22222222-2222-2222-2222-222222222222",
* "id": "11111111-1111-1111-1111-111111111111",
* "user_id": "11111111-1111-1111-1111-111111111111",
* "identity_data": {
* "email": "email@example.com",
* "email_verified": false,
* "phone_verified": false,
* "sub": "11111111-1111-1111-1111-111111111111"
* },
* "provider": "email",
* "last_sign_in_at": "2024-01-01T00:00:00Z",
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "email": "email@example.com"
* }
* ],
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "is_anonymous": false
* }
* },
* "error": null
* }
* ```
*
* @example Generate an invite link
* ```js
* const { data, error } = await supabase.auth.admin.generateLink({
* type: 'invite',
* email: 'email@example.com'
* })
* ```
*
* @example Generate a magic link
* ```js
* const { data, error } = await supabase.auth.admin.generateLink({
* type: 'magiclink',
* email: 'email@example.com'
* })
* ```
*
* @example Generate a recovery link
* ```js
* const { data, error } = await supabase.auth.admin.generateLink({
* type: 'recovery',
* email: 'email@example.com'
* })
* ```
*
* @example Generate links to change current email address
* ```js
* // generate an email change link to be sent to the current email address
* const { data, error } = await supabase.auth.admin.generateLink({
* type: 'email_change_current',
* email: 'current.email@example.com',
* newEmail: 'new.email@example.com'
* })
*
* // generate an email change link to be sent to the new email address
* const { data, error } = await supabase.auth.admin.generateLink({
* type: 'email_change_new',
* email: 'current.email@example.com',
* newEmail: 'new.email@example.com'
* })
* ```
*/
async generateLink(params) {
try {
const { options } = params, rest = __rest(params, ["options"]);
const body = Object.assign(Object.assign({}, rest), options);
if ('newEmail' in rest) {
// replace newEmail with new_email in request body
body.new_email = rest === null || rest === void 0 ? void 0 : rest.newEmail;
delete body['newEmail'];
}
return await _request(this.fetch, 'POST', `${this.url}/admin/generate_link`, {
body: body,
headers: this.headers,
xform: _generateLinkResponse,
redirectTo: options === null || options === void 0 ? void 0 : options.redirectTo,
});
}
catch (error) {
if (isAuthError(error)) {
return {
data: {
properties: null,
user: null,
},
error,
};
}
throw error;
}
}
// User Admin API
/**
* Creates a new user.
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*
* @category Auth
* @subcategory Auth Admin
*
* @remarks
* - To confirm the user's email address or phone number, set `email_confirm` or `phone_confirm` to true. Both arguments default to false.
* - `createUser()` will not send a confirmation email to the user. You can use [`inviteUserByEmail()`](/docs/reference/javascript/auth-admin-inviteuserbyemail) if you want to send them an email invite instead.
* - If you are sure that the created user's email or phone number is legitimate and verified, you can set the `email_confirm` or `phone_confirm` param to `true`.
*
* @example With custom user metadata
* ```js
* const { data, error } = await supabase.auth.admin.createUser({
* email: 'user@email.com',
* password: 'password',
* user_metadata: { name: 'Yoda' }
* })
* ```
*
* @exampleResponse With custom user metadata
* ```json
* {
* data: {
* user: {
* id: '1',
* aud: 'authenticated',
* role: 'authenticated',
* email: 'example@email.com',
* email_confirmed_at: '2024-01-01T00:00:00Z',
* phone: '',
* confirmation_sent_at: '2024-01-01T00:00:00Z',
* confirmed_at: '2024-01-01T00:00:00Z',
* last_sign_in_at: '2024-01-01T00:00:00Z',
* app_metadata: {},
* user_metadata: {},
* identities: [
* {
* "identity_id": "22222222-2222-2222-2222-222222222222",
* "id": "1",
* "user_id": "1",
* "identity_data": {
* "email": "example@email.com",
* "email_verified": true,
* "phone_verified": false,
* "sub": "1"
* },
* "provider": "email",
* "last_sign_in_at": "2024-01-01T00:00:00Z",
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "email": "email@example.com"
* },
* ],
* created_at: '2024-01-01T00:00:00Z',
* updated_at: '2024-01-01T00:00:00Z',
* is_anonymous: false,
* }
* }
* error: null
* }
* ```
*
* @example Auto-confirm the user's email
* ```js
* const { data, error } = await supabase.auth.admin.createUser({
* email: 'user@email.com',
* email_confirm: true
* })
* ```
*
* @example Auto-confirm the user's phone number
* ```js
* const { data, error } = await supabase.auth.admin.createUser({
* phone: '1234567890',
* phone_confirm: true
* })
* ```
*/
async createUser(attributes) {
try {
return await _request(this.fetch, 'POST', `${this.url}/admin/users`, {
body: attributes,
headers: this.headers,
xform: _userResponse,
});
}
catch (error) {
if (isAuthError(error)) {
return { data: { user: null }, error };
}
throw error;
}
}
/**
* Get a list of users.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
* @param params An object which supports `page` and `perPage` as numbers, to alter the paginated results.
*
* @category Auth
* @subcategory Auth Admin
*
* @remarks
* - Defaults to return 50 users per page.
*
* @example Get a page of users
* ```js
* const { data: { users }, error } = await supabase.auth.admin.listUsers()
* ```
*
* @example Paginated list of users
* ```js
* const { data: { users }, error } = await supabase.auth.admin.listUsers({
* page: 1,
* perPage: 1000
* })
* ```
*/
async listUsers(params) {
var _a, _b, _c, _d, _e, _f, _g;
try {
const pagination = { nextPage: null, lastPage: 0, total: 0 };
const response = await _request(this.fetch, 'GET', `${this.url}/admin/users`, {
headers: this.headers,
noResolveJson: true,
query: {
page: (_b = (_a = params === null || params === void 0 ? void 0 : params.page) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '',
per_page: (_d = (_c = params === null || params === void 0 ? void 0 : params.perPage) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : '',
},
xform: _noResolveJsonResponse,
});
if (response.error)
throw response.error;
const users = await response.json();
const total = (_e = response.headers.get('x-total-count')) !== null && _e !== void 0 ? _e : 0;
const links = (_g = (_f = response.headers.get('link')) === null || _f === void 0 ? void 0 : _f.split(',')) !== null && _g !== void 0 ? _g : [];
if (links.length > 0) {
links.forEach((link) => {
const page = parseInt(link.split(';')[0].split('=')[1].substring(0, 1));
const rel = JSON.parse(link.split(';')[1].split('=')[1]);
pagination[`${rel}Page`] = page;
});
pagination.total = parseInt(total);
}
return { data: Object.assign(Object.assign({}, users), pagination), error: null };
}
catch (error) {
if (isAuthError(error)) {
return { data: { users: [] }, error };
}
throw error;
}
}
/**
* Get user by id.
*
* @param uid The user's unique identifier
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*
* @category Auth
* @subcategory Auth Admin
*
* @remarks
* - Fetches the user object from the database based on the user's id.
* - The `getUserById()` method requires the user's id which maps to the `auth.users.id` column.
*
* @example Fetch the user object using the access_token jwt
* ```js
* const { data, error } = await supabase.auth.admin.getUserById(1)
* ```
*
* @exampleResponse Fetch the user object using the access_token jwt
* ```json
* {
* data: {
* user: {
* id: '1',
* aud: 'authenticated',
* role: 'authenticated',
* email: 'example@email.com',
* email_confirmed_at: '2024-01-01T00:00:00Z',
* phone: '',
* confirmation_sent_at: '2024-01-01T00:00:00Z',
* confirmed_at: '2024-01-01T00:00:00Z',
* last_sign_in_at: '2024-01-01T00:00:00Z',
* app_metadata: {},
* user_metadata: {},
* identities: [
* {
* "identity_id": "22222222-2222-2222-2222-222222222222",
* "id": "1",
* "user_id": "1",
* "identity_data": {
* "email": "example@email.com",
* "email_verified": true,
* "phone_verified": false,
* "sub": "1"
* },
* "provider": "email",
* "last_sign_in_at": "2024-01-01T00:00:00Z",
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "email": "email@example.com"
* },
* ],
* created_at: '2024-01-01T00:00:00Z',
* updated_at: '2024-01-01T00:00:00Z',
* is_anonymous: false,
* }
* }
* error: null
* }
* ```
*/
async getUserById(uid) {
validateUUID(uid);
try {
return await _request(this.fetch, 'GET', `${this.url}/admin/users/${uid}`, {
headers: this.headers,
xform: _userResponse,
});
}
catch (error) {
if (isAuthError(error)) {
return { data: { user: null }, error };
}
throw error;
}
}
/**
* Updates the user data. Changes are applied directly without confirmation flows.
*
* @param uid The user's unique identifier
* @param attributes The data you want to update.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*
* @remarks
* **Important:** This is a server-side operation and does **not** trigger client-side
* `onAuthStateChange` listeners. The admin API has no connection to client state.
*
* To sync changes to the client after calling this method:
* 1. On the client, call `supabase.auth.refreshSession()` to fetch the updated user data
* 2. This will trigger the `TOKEN_REFRESHED` event and notify all listeners
*
* @example
* ```typescript
* // Server-side (Edge Function)
* const { data, error } = await supabase.auth.admin.updateUserById(
* userId,
* { user_metadata: { preferences: { theme: 'dark' } } }
* )
*
* // Client-side (to sync the changes)
* const { data, error } = await supabase.auth.refreshSession()
* // onAuthStateChange listeners will now be notified with updated user
* ```
*
* @see {@link GoTrueClient.refreshSession} for syncing admin changes to the client
* @see {@link GoTrueClient.updateUser} for client-side user updates (triggers listeners automatically)
*
* @category Auth
* @subcategory Auth Admin
*
* @example Updates a user's email
* ```js
* const { data: user, error } = await supabase.auth.admin.updateUserById(
* '11111111-1111-1111-1111-111111111111',
* { email: 'new@email.com' }
* )
* ```
*
* @exampleResponse Updates a user's email
* ```json
* {
* "data": {
* "user": {
* "id": "11111111-1111-1111-1111-111111111111",
* "aud": "authenticated",
* "role": "authenticated",
* "email": "new@email.com",
* "email_confirmed_at": "2024-01-01T00:00:00Z",
* "phone": "",
* "confirmed_at": "2024-01-01T00:00:00Z",
* "recovery_sent_at": "2024-01-01T00:00:00Z",
* "last_sign_in_at": "2024-01-01T00:00:00Z",
* "app_metadata": {
* "provider": "email",
* "providers": [
* "email"
* ]
* },
* "user_metadata": {
* "email": "example@email.com",
* "email_verified": false,
* "phone_verified": false,
* "sub": "11111111-1111-1111-1111-111111111111"
* },
* "identities": [
* {
* "identity_id": "22222222-2222-2222-2222-222222222222",
* "id": "11111111-1111-1111-1111-111111111111",
* "user_id": "11111111-1111-1111-1111-111111111111",
* "identity_data": {
* "email": "example@email.com",
* "email_verified": false,
* "phone_verified": false,
* "sub": "11111111-1111-1111-1111-111111111111"
* },
* "provider": "email",
* "last_sign_in_at": "2024-01-01T00:00:00Z",
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "email": "example@email.com"
* }
* ],
* "created_at": "2024-01-01T00:00:00Z",
* "updated_at": "2024-01-01T00:00:00Z",
* "is_anonymous": false
* }
* },
* "error": null
* }
* ```
*
* @example Updates a user's password
* ```js
* const { data: user, error } = await supabase.auth.admin.updateUserById(
* '6aa5d0d4-2a9f-4483-b6c8-0cf4c6c98ac4',
* { password: 'new_password' }
* )
* ```
*
* @example Updates a user's metadata
* ```js
* const { data: user, error } = await supabase.auth.admin.updateUserById(
* '6aa5d0d4-2a9f-4483-b6c8-0cf4c6c98ac4',
* { user_metadata: { hello: 'world' } }
* )
* ```
*
* @example Updates a user's app_metadata
* ```js
* const { data: user, error } = await supabase.auth.admin.updateUserById(
* '6aa5d0d4-2a9f-4483-b6c8-0cf4c6c98ac4',
* { app_metadata: { plan: 'trial' } }
* )
* ```
*
* @example Confirms a user's email address
* ```js
* const { data: user, error } = await supabase.auth.admin.updateUserById(
* '6aa5d0d4-2a9f-4483-b6c8-0cf4c6c98ac4',
* { email_confirm: true }
* )
* ```
*
* @example Confirms a user's phone number
* ```js
* const { data: user, error } = await supabase.auth.admin.updateUserById(
* '6aa5d0d4-2a9f-4483-b6c8-0cf4c6c98ac4',
* { phone_confirm: true }
* )
* ```
*
* @example Ban a user for 100 years
* ```js
* const { data: user, error } = await supabase.auth.admin.updateUserById(
* '6aa5d0d4-2a9f-4483-b6c8-0cf4c6c98ac4',
* { ban_duration: '876000h' }
* )
* ```
*/
async updateUserById(uid, attributes) {
validateUUID(uid);
try {
return await _request(this.fetch, 'PUT', `${this.url}/admin/users/${uid}`, {
body: attributes,
headers: this.headers,
xform: _userResponse,
});
}
catch (error) {
if (isAuthError(error)) {
return { data: { user: null }, error };
}
throw error;
}
}
/**
* Delete a user. Requires a `service_role` key.
*
* @param id The user id you want to remove.
* @param shouldSoftDelete If true, then the user will be soft-deleted from the auth schema. Soft deletion allows user identification from the hashed user ID but is not reversible.
* Defaults to false for backward compatibility.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*
* @category Auth
* @subcategory Auth Admin
*
* @remarks
* - The `deleteUser()` method requires the user's ID, which maps to the `auth.users.id` column.
*
* @example Removes a user
* ```js
* const { data, error } = await supabase.auth.admin.deleteUser(
* '715ed5db-f090-4b8c-a067-640ecee36aa0'
* )
* ```
*
* @exampleResponse Removes a user
* ```json
* {
* "data": {
* "user": {}
* },
* "error": null
* }
* ```
*/
async deleteUser(id, shouldSoftDelete = false) {
validateUUID(id);
try {
return await _request(this.fetch, 'DELETE', `${this.url}/admin/users/${id}`, {
headers: this.headers,
body: {
should_soft_delete: shouldSoftDelete,
},
xform: _userResponse,
});
}
catch (error) {
if (isAuthError(error)) {
return { data: { user: null }, error };
}
throw error;
}
}
async _listFactors(params) {
validateUUID(params.userId);
try {
const { data, error } = await _request(this.fetch, 'GET', `${this.url}/admin/users/${params.userId}/factors`, {
headers: this.headers,
xform: (factors) => {
return { data: { factors }, error: null };
},
});
return { data, error };
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
async _deleteFactor(params) {
validateUUID(params.userId);
validateUUID(params.id);
try {
const data = await _request(this.fetch, 'DELETE', `${this.url}/admin/users/${params.userId}/factors/${params.id}`, {
headers: this.headers,
});
return { data, error: null };
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Lists all OAuth clients with optional pagination.
* Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _listOAuthClients(params) {
var _a, _b, _c, _d, _e, _f, _g;
try {
const pagination = { nextPage: null, lastPage: 0, total: 0 };
const response = await _request(this.fetch, 'GET', `${this.url}/admin/oauth/clients`, {
headers: this.headers,
noResolveJson: true,
query: {
page: (_b = (_a = params === null || params === void 0 ? void 0 : params.page) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '',
per_page: (_d = (_c = params === null || params === void 0 ? void 0 : params.perPage) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : '',
},
xform: _noResolveJsonResponse,
});
if (response.error)
throw response.error;
const clients = await response.json();
const total = (_e = response.headers.get('x-total-count')) !== null && _e !== void 0 ? _e : 0;
const links = (_g = (_f = response.headers.get('link')) === null || _f === void 0 ? void 0 : _f.split(',')) !== null && _g !== void 0 ? _g : [];
if (links.length > 0) {
links.forEach((link) => {
const page = parseInt(link.split(';')[0].split('=')[1].substring(0, 1));
const rel = JSON.parse(link.split(';')[1].split('=')[1]);
pagination[`${rel}Page`] = page;
});
pagination.total = parseInt(total);
}
return { data: Object.assign(Object.assign({}, clients), pagination), error: null };
}
catch (error) {
if (isAuthError(error)) {
return { data: { clients: [] }, error };
}
throw error;
}
}
/**
* Creates a new OAuth client.
* Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _createOAuthClient(params) {
try {
return await _request(this.fetch, 'POST', `${this.url}/admin/oauth/clients`, {
body: params,
headers: this.headers,
xform: (client) => {
return { data: client, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Gets details of a specific OAuth client.
* Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _getOAuthClient(clientId) {
try {
return await _request(this.fetch, 'GET', `${this.url}/admin/oauth/clients/${clientId}`, {
headers: this.headers,
xform: (client) => {
return { data: client, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Updates an existing OAuth client.
* Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _updateOAuthClient(clientId, params) {
try {
return await _request(this.fetch, 'PUT', `${this.url}/admin/oauth/clients/${clientId}`, {
body: params,
headers: this.headers,
xform: (client) => {
return { data: client, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Deletes an OAuth client.
* Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _deleteOAuthClient(clientId) {
try {
await _request(this.fetch, 'DELETE', `${this.url}/admin/oauth/clients/${clientId}`, {
headers: this.headers,
noResolveJson: true,
});
return { data: null, error: null };
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Regenerates the secret for an OAuth client.
* Only relevant when the OAuth 2.1 server is enabled in Supabase Auth.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _regenerateOAuthClientSecret(clientId) {
try {
return await _request(this.fetch, 'POST', `${this.url}/admin/oauth/clients/${clientId}/regenerate_secret`, {
headers: this.headers,
xform: (client) => {
return { data: client, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Lists all custom providers with optional type filter.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _listCustomProviders(params) {
try {
const query = {};
if (params === null || params === void 0 ? void 0 : params.type) {
query.type = params.type;
}
return await _request(this.fetch, 'GET', `${this.url}/admin/custom-providers`, {
headers: this.headers,
query,
xform: (data) => {
var _a;
return { data: { providers: (_a = data === null || data === void 0 ? void 0 : data.providers) !== null && _a !== void 0 ? _a : [] }, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: { providers: [] }, error };
}
throw error;
}
}
/**
* Creates a new custom OIDC/OAuth provider.
*
* For OIDC providers, the server fetches and validates the OpenID Connect discovery document
* from the issuer's well-known endpoint (or the provided `discovery_url`) at creation time.
* This may return a validation error (`error_code: "validation_failed"`) if the discovery
* document is unreachable, not valid JSON, missing required fields, or if the issuer
* in the document does not match the expected issuer.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _createCustomProvider(params) {
try {
return await _request(this.fetch, 'POST', `${this.url}/admin/custom-providers`, {
body: params,
headers: this.headers,
xform: (provider) => {
return { data: provider, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Gets details of a specific custom provider by identifier.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _getCustomProvider(identifier) {
try {
return await _request(this.fetch, 'GET', `${this.url}/admin/custom-providers/${identifier}`, {
headers: this.headers,
xform: (provider) => {
return { data: provider, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Updates an existing custom provider.
*
* When `issuer` or `discovery_url` is changed on an OIDC provider, the server re-fetches and
* validates the discovery document before persisting. This may return a validation error
* (`error_code: "validation_failed"`) if the discovery document is unreachable, invalid, or
* the issuer does not match.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _updateCustomProvider(identifier, params) {
try {
return await _request(this.fetch, 'PUT', `${this.url}/admin/custom-providers/${identifier}`, {
body: params,
headers: this.headers,
xform: (provider) => {
return { data: provider, error: null };
},
});
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Deletes a custom provider.
*
* This function should only be called on a server. Never expose your `service_role` key in the browser.
*/
async _deleteCustomProvider(identifier) {
try {
await _request(this.fetch, 'DELETE', `${this.url}/admin/custom-providers/${identifier}`, {
headers: this.headers,
noResolveJson: true,
});
return { data: null, error: null };
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Lists all passkeys for a user.
*
* This function should only be called on a server. Never expose your secret key in the browser.
*
* Requires `auth.experimental.passkey: true`.
*/
async _adminListPasskeys(params) {
assertPasskeyExperimentalEnabled(this.experimental);
validateUUID(params.userId);
try {
return await _request(this.fetch, 'GET', `${this.url}/admin/users/${params.userId}/passkeys`, { headers: this.headers, xform: (data) => ({ data, error: null }) });
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
/**
* Deletes a user's passkey.
*
* This function should only be called on a server. Never expose your secret key in the browser.
*
* Requires `auth.experimental.passkey: true`.
*/
async _adminDeletePasskey(params) {
assertPasskeyExperimentalEnabled(this.experimental);
validateUUID(params.userId);
validateUUID(params.passkeyId);
try {
await _request(this.fetch, 'DELETE', `${this.url}/admin/users/${params.userId}/passkeys/${params.passkeyId}`, { headers: this.headers, noResolveJson: true });
return { data: null, error: null };
}
catch (error) {
if (isAuthError(error)) {
return { data: null, error };
}
throw error;
}
}
}
//# sourceMappingURL=GoTrueAdminApi.js.map |