jsonwebtokenは、JSON Web Tokens (JWT) を生成および検証するためのライブラリです。JWTは、ユーザーの認証情報を安全に伝達するためのトークン形式であり、特にAPIベースのアプリケーションで広く使用されています。passportと組み合わせることで、JWTを使用した認証フローを実装し、セキュアなAPIアクセスを提供することができます。
import { AuthenticationClient } from "auth0";
const auth0 = new AuthenticationClient({
domain: "{YOUR_TENANT_AND REGION}.auth0.com",
clientId: "{YOUR_CLIENT_ID}",
clientSecret: "{OPTIONAL_CLIENT_SECRET}",
});
Management API Client
The Auth0 Management API is meant to be used by back-end servers or trusted parties performing administrative tasks. Generally speaking, anything that can be done through the Auth0 dashboard (and more) can also be done through this API.
Initialize your client class with a domain and token:
import { ManagementClient } from "auth0";
const management = new ManagementClient({
domain: "{YOUR_TENANT_AND REGION}.auth0.com",
token: "{YOUR_API_V2_TOKEN}",
});
Or use client credentials:
import { ManagementClient } from "auth0";
const management = new ManagementClient({
domain: "{YOUR_TENANT_AND REGION}.auth0.com",
clientId: "{YOUR_CLIENT_ID}",
clientSecret: "{YOUR_CLIENT_SECRET}",
withCustomDomainHeader: "auth.example.com", // Optional: Auto-applies to whitelisted endpoints
});
UserInfo API Client
This client can be used to retrieve user profile information.
import { UserInfoClient } from "auth0";
const userInfo = new UserInfoClient({
domain: "{YOUR_TENANT_AND REGION}.auth0.com",
});
// Get user info with an access token
const userProfile = await userInfo.getUserInfo(accessToken);
Legacy Usage
If you are migrating from the legacy node-auth0 package (v4.x) or need to maintain compatibility with legacy code, you can use the legacy export which provides the node-auth0 v4.x API interface.
Installing Legacy Version
The legacy version (node-auth0 v4.x) is available through the /legacy export path:
// Import the legacy version (node-auth0 v4.x API)
import { ManagementClient, AuthenticationClient } from "auth0/legacy";
// Or using CommonJS
const { ManagementClient, AuthenticationClient } = require("auth0/legacy");
Legacy Configuration
The legacy API uses the node-auth0 v4.x configuration format and method signatures, which are different from the current v5 API:
Legacy Management Client
import { ManagementClient } from "auth0/legacy";
const management = new ManagementClient({
domain: "{YOUR_TENANT_AND REGION}.auth0.com",
clientId: "{YOUR_CLIENT_ID}",
clientSecret: "{YOUR_CLIENT_SECRET}",
scope: "read:users update:users",
});
// Legacy API methods use promise-based patterns (node-auth0 v4.x style)
management.users
.getAll()
.then((users) => console.log(users))
.catch((err) => console.error(err));
// Or with async/await
try {
const users = await management.users.getAll();
console.log(users);
} catch (err) {
console.error(err);
}
Some list endpoints are paginated. You can iterate through pages using default values:
import { ManagementClient } from "auth0";
const client = new ManagementClient({
domain: "your-tenant.auth0.com",
token: "YOUR_TOKEN",
});
// Using default pagination (page size defaults vary by endpoint)
let page = await client.actions.list();
for (const item of page.data) {
console.log(item);
}
while (page.hasNextPage()) {
page = await page.getNextPage();
for (const item of page.data) {
console.log(item);
}
}
Or you can explicitly control pagination using page and per_page parameters:
// Offset-based pagination (most endpoints)
let page = await client.actions.list({
page: 0, // Page number (0-indexed)
per_page: 25, // Number of items per page
});
for (const item of page.data) {
console.log(item);
}
while (page.hasNextPage()) {
page = await page.getNextPage();
for (const item of page.data) {
console.log(item);
}
}
Some endpoints use checkpoint pagination with from and take parameters:
// Checkpoint-based pagination (e.g., connections, organizations)
let page = await client.connections.list({
take: 50, // Number of items per page
});
for (const item of page.data) {
console.log(item);
}
while (page.hasNextPage()) {
page = await page.getNextPage();
for (const item of page.data) {
console.log(item);
}
}
Advanced
Additional Headers
If you would like to send additional headers as part of the request, use the headers request option.
To apply the custom domain header globally across your application, use the withCustomDomainHeader option when initializing the ManagementClient. This will automatically inject the header for all whitelisted endpoints.
Retries
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
as the request is deemed retryable and the number of retry attempts has not grown larger than the configured
retry limit (default: 2).
A request is deemed retryable when any of the following HTTP status codes is returned:
The SDK provides access to raw response data, including headers, through the .withRawResponse() method.
The .withRawResponse() method returns a promise that results to an object with a data and a rawResponse property.
While we value open-source contributions to this SDK, this library is generated programmatically.
Additions made directly to this library would have to be moved over to our generation code,
otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
an issue first to discuss with us!
On the other hand, contributions to the README are always very welcome!
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
What is Auth0?
Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout Why Auth0?
This project is licensed under the MIT license. See the LICENSE file for more info.