Skip to content

Latest commit

 

History

History
299 lines (242 loc) · 7.52 KB

EXAMPLES.md

File metadata and controls

299 lines (242 loc) · 7.52 KB

Examples

Authentication Client

Signup a user

import { AuthenticationClient } from 'auth0';

const auth = new AuthenticationClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});
const { data: user } = await auth.database.signUp({
  email: '{USER_EMAIL}',
  password: '{USER_PASSWORD}',
  connection: 'Username-Password-Authentication',
});

Use a client assertion

import { AuthenticationClient } from 'auth0';

const clientAssertionSigningKey = `-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----`;

const auth = new AuthenticationClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientAssertionSigningKey,
});

const { data: tokens } = await auth.oauth.clientCredentialsGrant({
  audience: 'you-api',
});

Use Refresh Tokens

import { AuthenticationClient } from 'auth0';

const auth = new AuthenticationClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

// Get a new access token
const {
  data: { access_token },
} = await auth.oauth.refreshTokenGrant({
  refresh_token: refreshToken,
});

// Revoke a refresh token
await auth.oauth.revokeRefreshToken({
  token: refreshToken,
});

Complete the Authorization Code flow with PKCE

import { AuthenticationClient } from 'auth0';

const auth = new AuthenticationClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});
const { data: tokens } = await auth.oauth.authorizationCodeGrantWithPKCE(
  {
    code_verifier: '{key used to generate the code_challenge passed to /authorize}',
    code: '{code from authorization response}',
    redirect_uri: '{application redirect uri}',
  },
  {
    idTokenValidateOptions: {
      nonce: '{random string passed to /authorize to check against the nonce claim}',
      maxAge: '{number of seconds to check against the auth_time claim}',
      organization: '{organization name or ID to check against the org_id or org_name claim}',
    },
  }
);

Note: We recommend one of our Regular Web Application SDK Libraries for this.

Login with Passwordless

import { AuthenticationClient } from 'auth0';

const auth = new AuthenticationClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

// Or you can `sendSMS`
await auth.passwordless.sendEmail({
  email: '{user email}',
  send: 'code',
});

const { data: tokens } = await auth.passwordless.loginWithEmail({
  email: '{user email}',
  code: '{code from email}',
});

mTLS request

Refer mTLS documentation for more info - Link

import { AuthenticationClient } from 'auth0';
const { Agent } = require('undici');

const auth = new AuthenticationClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  agent: new Agent({
    connect: { cert: 'your_cert', key: 'your_key' },
  }),
  useMTLS: true,
});

const { data: tokens } = await auth.oauth.clientCredentialsGrant({
  audience: 'you-api',
});

Management Client

Paginate through a list of users

import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

const allUsers = [];
let page = 0;
while (true) {
  const {
    data: { users, total },
  } = await management.users.getAll({
    include_totals: true,
    page: page++,
  });
  allUsers.push(...users);
  if (allUsers.length === total) {
    break;
  }
}

Note: The maximum number of users you can get with this endpoint is 1000. For more, use users.exportUsers.

Paginate through a list of logs using checkpoint pagination

import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

const allLogs = [];
let from = '';
while (true) {
  const { data: logs } = await management.logs.getAll({ from });
  if (!logs.length) {
    break;
  }
  allLogs.push(...logs);
  ({ log_id: from } = logs[logs.length - 1]);
}

Import users from a JSON file

import { ManagementClient } from 'auth0';
import { fileFrom } from 'fetch-blob/from.js';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

const {
  data: { job: id },
} = await management.jobs.importUsers({
  users: await fileFrom('./users.json', 'application/json'),
  connection_id: 'con_{your connection id}',
});

let done = false;
while (!done) {
  const {
    data: {
      job: { status },
    },
  } = await management.jobs.get({ id });
  if (status === 'completed') {
    done = true;
  } else if (status === 'failed') {
    const { data: errors } = await management.jobs.getErrors({ id });
    throw new Error(errors);
  } else {
    await new Promise((resolve) => setTimeout(resolve, 2000));
  }
}

Update a user's user_metadata

import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

await management.users.update({ id: '{user id}' }, { user_metadata: { foo: 'bar' } });

Customizing the request

Passing custom options to fetch

import https from 'https';
import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
  headers: { 'foo': 'applied to all requests' },
  agent: new https.Agent({ ... }),
  timeoutDuration: 5000
});

await management.users.get({ id: '{user id}' }, { headers: { 'bar': 'applied to this request' } });

Overriding fetch

import { ManagementClient } from 'auth0';
import { myFetch } from './fetch';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
  async fetch(url, init) {
    log('before', url, init.method);
    const res = await myFetch(url, init);
    log('after', url, init.method, res.status);
    return res;
  },
});

await management.users.get({ id: '{user id}' });