{"id":13515918,"url":"https://github.com/netlify/gotrue-js","last_synced_at":"2025-05-14T10:13:23.437Z","repository":{"id":38955737,"uuid":"58991903","full_name":"netlify/gotrue-js","owner":"netlify","description":"JavaScript client library for GoTrue","archived":false,"fork":false,"pushed_at":"2025-04-08T15:58:06.000Z","size":4651,"stargazers_count":479,"open_issues_count":60,"forks_count":66,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-04-10T20:25:09.450Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/netlify.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-17T05:03:45.000Z","updated_at":"2025-04-05T07:54:42.000Z","dependencies_parsed_at":"2024-07-31T23:12:19.960Z","dependency_job_id":"4b173cb5-629b-4692-984e-970a0c37089f","html_url":"https://github.com/netlify/gotrue-js","commit_stats":{"total_commits":681,"total_committers":30,"mean_commits":22.7,"dds":"0.43171806167400884","last_synced_commit":"59879aef736510125b2242d3b7f7f28684266c10"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netlify%2Fgotrue-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netlify%2Fgotrue-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netlify%2Fgotrue-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netlify%2Fgotrue-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/netlify","download_url":"https://codeload.github.com/netlify/gotrue-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248292080,"owners_count":21079373,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T05:01:17.401Z","updated_at":"2025-04-11T03:38:19.900Z","avatar_url":"https://github.com/netlify.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","TypeScript","Auth"],"sub_categories":["Providers"],"readme":"![gotrue-js library](gotrue-js.png)\n\n[![Build Status](https://travis-ci.org/netlify/gotrue-js.svg?branch=main)](https://travis-ci.org/netlify/gotrue-js)\n\nThis is a JavaScript client library for the [GoTrue](https://github.com/netlify/gotrue) API.\n\nIt lets you create and authenticate users and is a building block for constructing\nthe UI for signups, password recovery, login and logout.\n\nPlay around the methods via the [demo site](https://gotruejs-playground.netlify.com/).\n\n## Installation\n\n```bash\nyarn add gotrue-js\n```\n\n## Usage\n\n```js\nimport GoTrue from 'gotrue-js';\n\n// Instantiate the GoTrue auth client with an optional configuration\n\nauth = new GoTrue({\n  APIUrl: 'https://\u003cyour domain name\u003e/.netlify/identity',\n  audience: '',\n  setCookie: false,\n});\n```\n\n### GoTrue configuration\n\nAPIUrl: The absolute path of the GoTrue endpoint. To find the `APIUrl`, go to `Identity` page of your Netlify site dashboard.\n\naudience(optional): `audience` is one of the pre-defined [JWT payload](https://tools.ietf.org/html/rfc7519#section-4.1.3) claims. It's an optional attribute which is set to be empty by default. If you were hosting your own identity service and wanted to support [multitenancy](https://en.wikipedia.org/wiki/Multitenancy), you would need `audience` to separate the users.\n\nsetCookie(optional): set to be `false` by default. If you wish to implement the `remember me` functionality, set the value to be `true`.\n\n### Error handling\n\nIf an error occurs during the request, the promise may be rejected with an Error, `HTTPError`, `TextHTTPError`, or `JSONHTTPError`. See [micro-api-client-lib error types](https://github.com/netlify/micro-api-client-lib#class-httperror-extends-error).\n\n## Authentication examples\n\n### Create a new user\n\nCreate a new user with the specified email and password\n\n```js\nauth.signup(email, password);\n```\n\nExample usage:\n\n```js\nauth\n  .signup(email, password)\n  .then((response) =\u003e console.log('Confirmation email sent', response))\n  .catch((error) =\u003e console.log(\"It's an error\", error));\n```\n\nExample response object:\n\n```json\n{\n  \"id\": \"example-id\",\n  \"aud\": \"\",\n  \"role\": \"\",\n  \"email\": \"example@example.com\",\n  \"confirmation_sent_at\": \"2018-04-27T22:36:59.636416916Z\",\n  \"app_metadata\": { \"provider\": \"email\" },\n  \"user_metadata\": null,\n  \"created_at\": \"2018-04-27T22:36:59.632133283Z\",\n  \"updated_at\": \"2018-04-27T22:37:00.061039863Z\"\n}\n```\n\nAlso, make sure the `Registration preferences` under `Identity settings` in your Netlify dashboard are set to `Open`.\n\n![registration preferences](src/images/identity-settings-registration.png)\n\nIf the registration preferences is set to be `Invite only`, you'll get an error message like this:\n`{code: 403, msg: 'Signups not allowed for this instance'}`\n\n### Confirm a new user signup\n\nThis function confirms a user sign up via a unique confirmation token\n\n```js\nauth.confirm(token, remember);\n```\n\nWhen a new user signed up, a confirmation email will be sent to the user if `Autoconfirm` isn't turned on under the [identity settings](https://www.netlify.com/docs/identity/#adding-users).\n\nIn the email, there's a link that says \"Confirm your email address\".\nWhen a user clicks on the link, it'll be redirected to the site with a [fragment identifier](https://en.wikipedia.org/wiki/Fragment_identifier) `#confirmation_token=Iyo9xHvsGVbW-9A9v4sDmQ` in the URL.\n\nFor all good reasons, the `confirmation_token` is hidden from the browser via a redirect.\n\nIf you wish to manually confirm a user using the `auth.confirm(token, remember)` method,\nyou can copy the link location of the email and use the `curl -I` script to get the `confirmation_token` from your terminal. E.g.,\n\n```bash\n$ curl -I https://mandrillapp.com/track/click/30002868/example.netlify.com?p=example-token\n  HTTP/1.1 302 Moved Temporarily\n  Server: nginx/1.12.2\n  Date: Tue, 15 May 2018 21:19:13 GMT\n  Content-Type: text/html; charset=utf-8\n  Set-Cookie: PHPSESSID=77c421bf85fa412e5f994f28a6b30956; expires=Wed, 16-May-2018 07:19:13 GMT; path=/; secure; HttpOnly\n  Expires: Thu, 19 Nov 1981 08:52:00 GMT\n  Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\n  Pragma: no-cache\n  Set-Cookie: PHPSESSID=77c421bf85fa412e5f994f28a6b30956; expires=Wed, 16-May-2018 07:19:13 GMT; path=/; secure; httponly\n  Location: https://example.netlify.com/#confirmation_token=Iyo9xHvsGVbW-9A9v4sDmQ\n```\n\nExample usage:\n\n```js\nauth\n  .confirm(token, true)\n  .then((response) =\u003e {\n    console.log('Confirmation email sent', JSON.stringify({ response }));\n  })\n  .catch((error) =\u003e {\n    console.log(error);\n  });\n```\n\n_This method requires usage of browser window object `localStorage`. Test the usage in your front end code._\n\nExample response object:\n\n```json\n{\n  \"response\": {\n    \"api\": {\n      \"apiURL\": \"https://example.netlify.com/.netlify/identity\",\n      \"_sameOrigin\": true,\n      \"defaultHeaders\": {}\n    },\n    \"url\": \"https://example.netlify.com/.netlify/identity\",\n    \"token\": {\n      \"access_token\": \"example-jwt-token\",\n      \"token_type\": \"bearer\",\n      \"expires_in\": 3600,\n      \"refresh_token\": \"example-refresh_token\",\n      \"expires_at\": 1526110512000\n    },\n    \"id\": \"example-id\",\n    \"aud\": \"\",\n    \"role\": \"\",\n    \"email\": \"example@netlify.com\",\n    \"confirmed_at\": \"2018-05-12T06:35:13Z\",\n    \"confirmation_sent_at\": \"2018-05-12T06:34:35Z\",\n    \"app_metadata\": {\n      \"provider\": \"email\"\n    },\n    \"user_metadata\": {},\n    \"created_at\": \"2018-05-12T06:34:35Z\",\n    \"updated_at\": \"2018-05-12T06:34:35Z\"\n  }\n}\n```\n\n### Login a user\n\nHandles user login via the specified email and password\n\n`auth.login(email, password, remember)`\n\nExample usage:\n\n```js\nauth\n  .login(email.value, password.value, true)\n  .then((response) =\u003e {\n    showMessage(`Success! Response: ${JSON.stringify({ response })}`, form);\n  })\n  .catch((error) =\u003e showMessage(`Failed :( ${JSON.stringify(error)}`, form));\n```\n\nExample response object:\n\n```json\n{\n  \"response\": {\n    \"api\": {\n      \"apiURL\": \"https://example.netlify.com/.netlify/identity\",\n      \"_sameOrigin\": true,\n      \"defaultHeaders\": {}\n    },\n    \"url\": \"https://example.netlify.com/.netlify/identity\",\n    \"token\": {\n      \"access_token\": \"example-jwt-token\",\n      \"token_type\": \"bearer\",\n      \"expires_in\": 3600,\n      \"refresh_token\": \"example-refresh_token\",\n      \"expires_at\": 1526062471000\n    },\n    \"id\": \"example-id\",\n    \"aud\": \"\",\n    \"role\": \"\",\n    \"email\": \"example@netlify.com\",\n    \"confirmed_at\": \"2018-05-04T23:57:17Z\",\n    \"app_metadata\": {\n      \"provider\": \"email\"\n    },\n    \"user_metadata\": {},\n    \"created_at\": \"2018-05-04T23:57:17Z\",\n    \"updated_at\": \"2018-05-04T23:57:17Z\"\n  }\n}\n```\n\n### Request password recover email\n\nThis function sends a request to GoTrue API and triggers a password recovery email to the specified email address.\nSimilar to `confirmation_token`, the `recovery_token` is baked in the link of the email. You can also copy the link location from the email and run `curl -I` in the command line to grab the token.\n\n`auth.requestPasswordRecovery(email)`\n\nExample usage:\n\n```js\nauth\n  .requestPasswordRecovery(email)\n  .then((response) =\u003e console.log('Recovery email sent', { response }))\n  .catch((error) =\u003e console.log('Error sending recovery mail: %o', error));\n```\n\nExample response object:\n`{}`\n\n### Recover a user account\n\nThis function recovers a user account via a recovery token\n\n`auth.recover(recoveryToken, remember)`\n\nExample usage:\n\n```js\nauth\n  .recover(token, true)\n  .then((response) =\u003e console.log('Logged in as %s', JSON.stringify({ response })))\n  .catch((error) =\u003e console.log('Failed to verify recover token: %o', error));\n```\n\nExample response object:\n\n```json\n{\n  \"response\": {\n    \"api\": {\n      \"apiURL\": \"https://example.netlify.com/.netlify/identity\",\n      \"_sameOrigin\": true,\n      \"defaultHeaders\": {}\n    },\n    \"url\": \"https://example.netlify.com/.netlify/identity\",\n    \"token\": {\n      \"access_token\": \"example-jwt-token\",\n      \"token_type\": \"bearer\",\n      \"expires_in\": 3600,\n      \"refresh_token\": \"example-refresh_token\",\n      \"expires_at\": 1526107729000\n    },\n    \"id\": \"example-id\",\n    \"aud\": \"\",\n    \"role\": \"\",\n    \"email\": \"example@netlify.com\",\n    \"confirmed_at\": \"2018-05-12T05:48:49Z\",\n    \"invited_at\": \"2018-05-04T23:40:00Z\",\n    \"recovery_sent_at\": \"2018-05-12T05:48:13Z\",\n    \"app_metadata\": {\n      \"provider\": \"email\"\n    },\n    \"user_metadata\": {},\n    \"created_at\": \"2018-05-04T23:40:00Z\",\n    \"updated_at\": \"2018-05-04T23:40:00Z\"\n  }\n}\n```\n\n### Get current user\n\nThis function returns the current user object when a user is logged in\n\n`auth.currentUser()`\n\nExample usage:\n\n```js\nconst user = auth.currentUser();\n```\n\nExample response object:\n\n```json\n{\n  \"api\": {\n    \"apiURL\": \"https://example.netlify.com/.netlify/identity\",\n    \"_sameOrigin\": true,\n    \"defaultHeaders\": {}\n  },\n  \"url\": \"https://example.netlify.com/.netlify/identity\",\n  \"token\": {\n    \"access_token\": \"example-jwt-token\",\n    \"token_type\": \"bearer\",\n    \"expires_in\": 3600,\n    \"refresh_token\": \"example-refresh_token\",\n    \"expires_at\": 1525214326000\n  },\n  \"id\": \"example-id\",\n  \"aud\": \"\",\n  \"role\": \"\",\n  \"email\": \"example@example.com\",\n  \"confirmed_at\": \"2018-05-01T19:21:00Z\",\n  \"app_metadata\": {\n    \"provider\": \"email\"\n  },\n  \"user_metadata\": {},\n  \"created_at\": \"2018-05-01T19:21:00Z\",\n  \"updated_at\": \"2018-05-01T19:21:00Z\"\n}\n```\n\n### Update a user\n\nThis function updates a user object with specified attributes\n\n`user.update(attributes)`\n\nUsers can update their `user_metadata` field. To do this, pass an object to the `attributes.data` key with the fields you want to update. Updates to a users `app_metadata` must be performed from a secure environment, such as a [Lambda function](https://www.netlify.com/docs/functions/). For examples on updating user and app metadata, see [netlify/identity-update-user-data](https://github.com/netlify/identity-update-user-data).\n\nExample usage:\n\n```js\nconst user = auth.currentUser();\n\nuser\n  .update({ email: 'example@example.com', password: 'password' })\n  .then((user) =\u003e console.log('Updated user %s', user))\n  .catch((error) =\u003e {\n    console.log('Failed to update user: %o', error);\n    throw error;\n  });\n```\n\nExample response object:\n\n```json\n{\n  \"api\": {\n    \"apiURL\": \"https://example.netlify.com/.netlify/identity\",\n    \"_sameOrigin\": true,\n    \"defaultHeaders\": {}\n  },\n  \"url\": \"https://example.netlify.com/.netlify/identity\",\n  \"token\": {\n    \"access_token\": \"example-jwt-token\",\n    \"token_type\": \"bearer\",\n    \"expires_in\": 3600,\n    \"refresh_token\": \"example-refresh_token\",\n    \"expires_at\": 1525215471000\n  },\n  \"id\": \"example-id\",\n  \"aud\": \"\",\n  \"role\": \"\",\n  \"email\": \"example@example.com\",\n  \"confirmed_at\": \"2018-05-01T19:21:00Z\",\n  \"app_metadata\": {\n    \"provider\": \"email\"\n  },\n  \"user_metadata\": {},\n  \"created_at\": \"2018-05-01T19:21:00Z\",\n  \"updated_at\": \"2018-05-01T22:04:07.923944421Z\",\n  \"new_email\": \"new-example@example.com\",\n  \"email_change_sent_at\": \"2018-05-01T22:04:07.49197052Z\"\n}\n```\n\n### Get a JWT token\n\nThis function retrieves a JWT token from a currently logged in user\n\n`user.jwt(forceRefresh)`\n\nExample usage:\n\n```js\nconst user = auth.currentUser();\nconst jwt = user.jwt();\njwt\n  .then((response) =\u003e console.log('This is a JWT token', response))\n  .catch((error) =\u003e {\n    console.log('Error fetching JWT token', error);\n    throw error;\n  });\n```\n\nExample response object:\n\n```bash\neyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjUyMTk4MTYsInN1YiI6ImE5NG.98YDkB6B9JbBlDlqqef2nme2tkAnsi30QVys9aevdCw debugger eval code:1:43\n```\n\n### Logout a user\n\nThis function removes the current session of the user and log out the user\n\n`user.logout()`\n\nExample usage:\n\n```js\nconst user = auth.currentUser();\nuser\n  .logout()\n  .then(response =\u003e console.log(\"User logged out\");)\n  .catch(error =\u003e {\n    console.log(\"Failed to logout user: %o\", error);\n    throw error;\n  });\n```\n\n## Admin methods\n\nThe following admin methods are currently not available to be used directly. You can access `context.clientContext.identity` and get a short lived admin token through a Lambda function and achieve the same goals, e.g., update user role, create or delete user etc. See [Functions and Identity](https://docs.netlify.com/functions/functions-and-identity/) for more info.\n\n\u003e For users of [Netlify CLI](https://github.com/netlify/cli) - functions using admin methods will _not_ work locally - they need to be deployed to your site in order to work as intended.\n\nLet's create a simple login form in HTML and JavaScript to interact with a lambda function and test out the admin methods.\n\n1. Create an HTML form for user login\n\n```html\n\u003ch2\u003eLog in\u003c/h2\u003e\n\u003cform name=\"login\"\u003e\n  \u003cdiv class=\"message\"\u003e\u003c/div\u003e\n  \u003cp\u003e\n    \u003clabel\u003eEmail\u003cbr /\u003e\u003cinput type=\"email\" name=\"email\" required /\u003e\u003c/label\u003e\n  \u003c/p\u003e\n  \u003cp\u003e\n    \u003clabel\u003ePassword\u003cbr /\u003e\u003cinput type=\"password\" name=\"password\" required /\u003e\u003c/label\u003e\n  \u003c/p\u003e\n  \u003cbutton type=\"submit\"\u003eLog in\u003c/button\u003e\n\u003c/form\u003e\n```\n\n2. Invoke lambda function. (In this example our function is names as `hello.js`)\n\n```js\ndocument.querySelector(\"form[name='login']\").addEventListener(\"submit\", e =\u003e {\n  e.preventDefault();\n  const form = e.target;\n  const { email, password } = form.elements;\n  auth\n    .login(email.value, password.value, true)\n    .then(response =\u003e {\n      const myAuthHeader = \"Bearer \" + response.token.access_token; //creates the bearer token\n      fetch(\"/.netlify/functions/hello\", {\n        headers: { Authorization: myAuthHeader },\n        credentials: \"include\"\n      })\n        .then(response =\u003e {\n          console.log({ response });\n        })\n        .catch(error =\u003e {...});\n    })\n    .catch(error =\u003e {...});\n});\n```\n\n### Get a user\n\nThis function retrieves a user object with the specified user id\n\n```js\ngetUser(user) {\n    return this.user.\\_request(`/admin/users/${user.id}`);\n}\n```\n\nExample usage:\n\n```js\nimport fetch from 'node-fetch';\n\nexports.handler = async (event, context) =\u003e {\n  const { identity, user } = context.clientContext;\n  const userID = user.sub;\n  const userUrl = `${identity.url}/admin/users/{${userID}}`;\n  const adminAuthHeader = `Bearer ${  identity.token}`;\n\n  try {\n    return fetch(userUrl, {\n      method: 'GET',\n      headers: { Authorization: adminAuthHeader },\n    })\n      .then((response) =\u003e response.json())\n      .then((data) =\u003e {\n        console.log('data', JSON.stringify(data));\n        return { statusCode: 204 };\n      })\n      .catch((error) =\u003e {\n        console.log('Failed to get user! 500! Internal.');\n        return {\n          statusCode: 500,\n          body: `Internal Server Error: ${  error}`,\n        };\n      });\n  } catch (error) {\n    console.log('GOT HERE! 500! outer');\n    return { statusCode: 500, body: `Internal Server Error: ${  error}` };\n  }\n};\n```\n\nExample response object:\n\n```json\n{\n  \"id\": \"example-id\",\n  \"aud\": \"\",\n  \"role\": \"\",\n  \"email\": \"example@netlify.com\",\n  \"confirmed_at\": \"2018-05-09T06:28:46Z\",\n  \"app_metadata\": {\n    \"provider\": \"email\"\n  },\n  \"user_metadata\": {},\n  \"created_at\": \"2018-05-09T06:28:46Z\",\n  \"updated_at\": \"2018-05-09T06:28:46Z\"\n}\n```\n\n### Update a user\n\nThis function updates the an existing user with the specified attributes\n\n```js\nupdateUser(user, attributes = {}) {\n   return this.user._request(`/admin/users/${user.id}`, {\n     method: \"PUT\",\n     body: JSON.stringify(attributes)\n   });\n}\n```\n\nExample usage:\n\n```js\nimport fetch from \"node-fetch\";\n\nexports.handler = async (event, context) =\u003e {\n  const { identity, user } = context.clientContext;\n  const userID = user.sub;\n  const userUrl = `${identity.url}/admin/users/${userID}`;\n  const adminAuthHeader = \"Bearer \" + identity.token;\n\n  try {\n    return fetch(userUrl, {\n      method: \"PUT\",\n      headers: { Authorization: adminAuthHeader },\n      body: JSON.stringify({ app_metadata: { roles: [\"superstar\"] } })\n    })\n      .then(response =\u003e {\n        return response.json();\n      })\n      .then(data =\u003e {\n        console.log(\"Updated a user! 204!\");\n        console.log(JSON.stringify({ data }));\n        return { statusCode: 204 };\n      })\n      .catch(e =\u003e return {...});\n  } catch (e) { return e; }\n};\n```\n\nExample response object:\n\n```json\n{\n  \"data\": {\n    \"id\": \"example-id\",\n    \"aud\": \"\",\n    \"role\": \"\",\n    \"email\": \"example@netlify.com\",\n    \"confirmed_at\": \"2018-05-09T06:52:58Z\",\n    \"app_metadata\": {\n      \"provider\": \"email\",\n      \"roles\": [\n        \"superstar\"\n      ]\n    },\n    \"user_metadata\": {},\n    \"created_at\": \"2018-05-09T06:52:58Z\",\n    \"updated_at\": \"2018-05-11T00:26:27.668465915Z\"\n  }\n}\n```\n\n### Invite a user\n\nTo invite a user using the admin token, do a `POST` request to `/invite` endpoint. It's not possible to set `user_metadata` or `app_metadata` until a user has been created.\n\nExample usage:\n\n```js\nimport fetch from 'node-fetch';\n\nexports.handler = async (event, context) =\u003e {\n  const { identity } = context.clientContext;\n  const inviteUrl = `${identity.url}/invite`;\n  const adminAuthHeader = \"Bearer \" + identity.token;\n\n    try {\n      return fetch(inviteUrl, {\n        method: \"POST\",\n        headers: { Authorization: adminAuthHeader },\n        body: JSON.stringify({ email: \"example@example.com\" })\n      })\n      .then(response =\u003e {\n        return response.json();\n      })\n      .then(data =\u003e {\n        console.log(\"Invited a user! 204!\");\n        console.log(JSON.stringify({ data }));\n        return { statusCode: 204 };\n      })\n      .catch(e =\u003e return {...});\n  } catch (e) { return e; };\n};\n```\n\nExample response:\n\n```json\n{\n  \"id\": \"example-id\",\n  \"aud\": \"\",\n  \"role\": \"\",\n  \"email\": \"example@example.com\",\n  \"invited_at\": \"2018-05-25T20:28:04.436230023Z\",\n  \"app_metadata\": {\n    \"provider\": \"email\"\n  },\n  \"user_metadata\": null,\n  \"created_at\": \"2018-05-25T20:28:03.684905861Z\",\n  \"updated_at\": \"2018-05-25T20:28:04.862592451Z\"\n}\n```\n\n### Create a new user\n\nThis function creates a new user object with the specified new email and password and other optional attributes. User will not be confirmed unless `confirm` parameter is set to true.\n\n```js\ncreateUser(email, password, attributes = {}) {\n    attributes.email = email;\n    attributes.password = password;\n    return this.user.\\_request(\"/admin/users\", {\n    method: \"POST\",\n    body: JSON.stringify(attributes)\n    });\n  }\n```\n\nExample usage:\n\n```js\nimport fetch from \"node-fetch\";\n\nexports.handler = async (event, context) =\u003e {\n  const { identity, user } = context.clientContext;\n  const userID = user.sub;\n  const usersUrl = `${identity.url}/admin/users`;\n  const adminAuthHeader = \"Bearer \" + identity.token;\n\n  try {\n    return fetch(usersUrl, {\n      method: \"POST\",\n      headers: { Authorization: adminAuthHeader },\n      body: JSON.stringify({ email: \"new-email@netlify.com\", password: \"newpw\", confirm: true })\n    })\n      .then(response =\u003e {\n        return response.json();\n      })\n      .then(data =\u003e {\n        console.log(\"Created a user! 204!\");\n        console.log(JSON.stringify({ data }));\n        return { statusCode: 204 };\n      })\n      .catch(e =\u003e {...};\n      });\n  } catch (e) {\n    return e;\n  }\n};\n```\n\nExample response object:\n\n```json\n{\n  \"data\": {\n    \"id\": \"new-id\",\n    \"aud\": \"\",\n    \"role\": \"\",\n    \"email\": \"new-email@netlify.com\",\n    \"app_metadata\": {\n      \"provider\": \"email\"\n    },\n    \"user_metadata\": null,\n    \"created_at\": \"2018-05-11T00:37:34.475713996Z\",\n    \"updated_at\": \"2018-05-11T00:37:34.481743781Z\"\n  }\n}\n```\n\n### Delete a user\n\nThis function deletes an existing user object\n\n```js\ndeleteUser(user) {\n  return this.user.\\_request(`/admin/users/${user.id}`, {\n  method: \"DELETE\"\n  });\n}\n```\n\nExample usage:\n\n```js\nimport fetch from 'node-fetch';\n\nexports.handler = async (event, context) =\u003e {\n  const { identity, user } = context.clientContext;\n  const userID = user.sub;\n  const userUrl = `${identity.url}/admin/users/{${userID}}`;\n  const adminAuthHeader = `Bearer ${identity.token}`;\n\n  try {\n    return fetch(userUrl, {\n      method: 'DELETE',\n      headers: { Authorization: adminAuthHeader },\n    })\n      .then((response) =\u003e {\n        console.log('Deleted a user!');\n        return response.json();\n      })\n      .then((data) =\u003e {\n        console.log({ data });\n        return { statusCode: 204 };\n      })\n      .catch((error) =\u003e ({\n          statusCode: 500,\n          body: `Internal Server Error: ${error}`,\n        }));\n  } catch (error) {\n    return error;\n  }\n};\n```\n\nExample response object:\n\n```json\n{ \"data\": {} }\n```\n\n### Get a list of users\n\nThis function retrieves an array of user objects. The `audience` param is optional. It's for when you are hosting your own identity service and want to support [multitenancy](https://en.wikipedia.org/wiki/Multitenancy).\n\n```js\nlistUsers(aud) {\n    return this.user._request(\"/admin/users\", {\n      method: \"GET\",\n      audience: aud\n    });\n}\n```\n\nExample usage:\n\n```js\nimport fetch from 'node-fetch';\n\nexports.handler = async (event, context) =\u003e {\n  const { identity, user } = context.clientContext;\n  const usersUrl = `${identity.url}/admin/users`;\n  const adminAuthHeader = `Bearer ${identity.token}`;\n\n  try {\n    return fetch(usersUrl, {\n      method: 'GET',\n      headers: { Authorization: adminAuthHeader },\n    })\n      .then((response) =\u003e response.json())\n      .then((data) =\u003e {\n        console.log('data', JSON.stringify(data));\n        return { statusCode: 204 };\n      })\n      .catch((error) =\u003e ({\n          statusCode: 500,\n          body: `Internal Server Error: ${error}`,\n        }));\n  } catch (error) {\n    return error;\n  }\n};\n```\n\nExample response object:\n\n```json\n{\n  \"aud\": \"\",\n  \"users\": [\n    {\n      \"id\": \"example-id-01\",\n      \"aud\": \"\",\n      \"role\": \"\",\n      \"email\": \"example-email-01@netlify.com\",\n      \"app_metadata\": {\n        \"provider\": \"email\"\n      },\n      \"user_metadata\": {},\n      \"created_at\": \"2018-05-09T18:14:51Z\",\n      \"updated_at\": \"2018-05-09T18:14:51Z\"\n    },\n    {\n      \"id\": \"example-id-02\",\n      \"aud\": \"\",\n      \"role\": \"\",\n      \"email\": \"example-email-02@netlify.com\",\n      \"confirmed_at\": \"2018-05-09T06:52:58Z\",\n      \"app_metadata\": {\n        \"provider\": \"email\"\n      },\n      \"user_metadata\": {},\n      \"created_at\": \"2018-05-09T06:52:58Z\",\n      \"updated_at\": \"2018-05-09T06:52:58Z\"\n    },\n    {\n      \"id\": \"example-id-03\",\n      \"aud\": \"\",\n      \"role\": \"\",\n      \"email\": \"example-email-03@netlify.com\",\n      \"confirmed_at\": \"2018-05-09T06:28:46Z\",\n      \"app_metadata\": {\n        \"provider\": \"email\",\n        \"roles\": [\n          \"admin\"\n        ]\n      },\n      \"user_metadata\": {},\n      \"created_at\": \"2018-05-09T06:28:46Z\",\n      \"updated_at\": \"2018-05-09T06:28:46Z\"\n    }\n  ]\n}\n```\n\n## Oauth providers supported by Netlify\n\nCurrently we support Google, GitHub, GitLab, and BitBucket as directly supported in the Netlify app UI (other oauth providers require serverless functions to be set up correctly, but these don't.)\n\n[`acceptInviteExternalUrl`](https://github.com/netlify/gotrue-js/blob/6dda47191dac9194658c5899272fc962d96f8cd6/index.d.ts#L21) and [`loginExternalUrl`](https://github.com/netlify/gotrue-js/blob/6dda47191dac9194658c5899272fc962d96f8cd6/index.d.ts#L26) are useful for that. You can see example usage in [`netlify-identity-widget`](https://github.com/netlify/netlify-identity-widget/blob/ece08ed2a3653adcad87cf3998277362e362da60/src/state/store.js#L99-L119)\n\n## See also\n\n- [gotrue](https://github.com/netlify/gotrue)\n- [netlify-identity-widget](https://github.com/netlify/netlify-identity-widget/)\n- [micro-api-client-library](https://github.com/netlify/micro-api-client-lib)\n- [Netlify identity docs](https://www.netlify.com/docs/identity/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetlify%2Fgotrue-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnetlify%2Fgotrue-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetlify%2Fgotrue-js/lists"}