GenAuth DocsDocuments
Concepts
User Guide
Development Integration
AgentAuth
Metadata
Development Integration
Multi-tenant (beta)
Console Documentation
Multi-tenant Console
Tenant Console
Saas Application Demo
Concepts
User Guide
Development Integration
AgentAuth
Metadata
Development Integration
Multi-tenant (beta)
Console Documentation
Multi-tenant Console
Tenant Console
Saas Application Demo
Old version
  • Single Page Web Application

  • Mobile and Client Applications

  • Standard Web Application

    • Java

    • Node.js

      • Installation and Usage
      • User Authentication Module
      • Management Module

    • Python

  • Framework Integration

  • Others

  1. Development Integration
  2. /
  3. Standard Web Application
  4. /
  5. Node.js
  6. /
  7. Installation and Usage

¶ 安装

Update time: 2025-07-23 07:34:21
Edit

¶ NPM

npm install @genauth/nodejs

¶ Yarn

yarn add @genauth/nodejs

Github 仓库: https://github.com/Authing/@genauth/nodejs (opens new window)

¶ 使用 管理模块

¶ 初始化

初始化 ManagementClient 需要使用 accessKeyId 和 accessKeySecret 参数:

import { ManagementClient } from "@genauth/nodejs";

const managementClient = new ManagementClient({
  accessKeyId: "YOUR_ACCESS_KEY_ID",
  accessKeySecret: "YOUR_ACCESS_KEY_SECRET",
});

ManagementClient 会自动从 GenAuth 服务器获取 Management API Token,并通过返回的 Token 过期时间自动对 Token 进行缓存。

完整的参数和释义如下:

  • accessKeyId: GenAuth 用户池 ID;
  • accessKeySecret: GenAuth 用户池密钥;
  • timeout: 超时时间,单位为 ms,默认为 10000 ms;
  • host: GenAuth 服务器地址,默认为 https://api.genauth.ai。如果你使用的是 GenAuth 公有云版本,请忽略此参数。如果你使用的是私有化部署的版本,此参数必填,格式如下: https://authing-api.my-authing-service.com(最后不带斜杠 /)。
  • lang: 接口 Message 返回语言格式(可选),可选值为 zh-CN 和 en-US,默认为 zh-CN。

¶ 快速开始

初始化完成 ManagementClient 之后,你可以获取 ManagementClient 的实例,然后调用此实例上的方法。例如:

  • 获取用户列表
(async () => {
  const { data } = await managementClient.listUsers({
    page: 1,
    limit: 10,
  });
})();
  • 创建角色
(async () => {
  const { data } = await managementClient.createRole({
    code: "admin",
    description: "管理员",
    namespace: "default",
  });
})();

完整的接口列表,你可以在 GenAuth Open API (opens new window) 和 SDK 文档 (opens new window) 中获取。

¶ 使用 认证模块

¶ 初始化

初始化 AuthenticationClient 需要使用 appId 、 appSecret 和 host 参数:

import { AuthenticationClient } from "@genauth/nodejs";

const authenticationClient = new AuthenticationClient({
  appId: "GEN_AUTH_APP_ID",
  appSecret: "YOUR_APP_SECRET",
  host: "YOUR_USERPOOL_HOST",
});

完整的参数和释义如下:

  • appId: GenAuth 应用 ID ;
  • appSecret: GenAuth 应用 Secret;
  • host: 应用对应的用户池域名,例如 pool.genauth.ai;
  • redirectUri: 认证完成后的重定向目标 URL, 会进行校验,需要和控制台的设置保持一致。
  • logoutRedirectUri: 登出完成后的重定向目标 URL。
  • scope: 令牌具备的资源权限(应用侧向 GenAuth 请求的权限),以空格分隔,默认为 'openid profile',成功获取的权限会出现在 Access Token 的 scope 字段中。更多 scope 定义参见 GenAuth 相关文档 (opens new window)。
  • serverJWKS: 服务端的 JWKS 公钥,用于验证 Token 签名,默认会通过网络请求从服务端的 JWKS 端点自动获取。
  • cookieKey: 存储认证上下文的 Cookie 名称,用于 方法 loginWithRedirect 和 handleRedirectCallback 上存储用户的认证状态。

认证侧相关的使用和方法说明,你可以在 GenAuth Nodejs SDK 中查看。

¶ 私有化部署

如果你使用的是私有化部署的 GenAuth IDaaS 服务,需要指定此 GenAuth 私有化实例的 host,如:

import { ManagementClient } from "@genauth/nodejs";

const managementClient = new ManagementClient({
  accessKeyId: "YOUR_ACCESS_KEY_ID",
  accessKeySecret: "YOUR_ACCESS_KEY_SECRET",
  host: "https://authing-api.my-authing-service.com",
});

如果你不清楚如何获取,可以联系 GenAuth IDaaS 服务管理员。

Previous article: Node.js Next article: User Authentication Module
  • 安装
  • 使用 管理模块
  • 使用 认证模块
  • 私有化部署

User identity management

Integrated third-party login
Customized authentication process

Enterprise internal management

Single sign-on
Multi-factor authentication
Permission management

Developer

Development Documentation
GitHub (opens new window)

Company

official@genauth.ai
16th Floor, Building B, Beichen Century Center, Chaoyang District, Beijing (Total)
Room 406, 4th Floor, Block B, No. 200, Tianfu Fifth Street, High-tech Zone, Chengdu (Branch)

© Beijing Steamory Technology Co., Ltd.