Authing 文档文档
快速开始
概念
使用指南
开发集成 arrow
  • V2 文档
  • V3 文档
元数据
应用集成
身份自动化
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
快速开始
概念
使用指南
开发集成 arrow
  • V2 文档
  • V3 文档
元数据
应用集成
身份自动化
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
旧版
  • 单页 Web 应用

  • 移动、客户端应用

  • 标准 Web 应用

    • C#

    • Go

    • Java

    • Node.js

      • 安装使用
      • 用户认证模块
      • 管理模块

    • PHP

    • Python

  • 框架集成

  • 其他

  1. 开发集成
  2. /
  3. 标准 Web 应用
  4. /
  5. Node.js
  6. /
  7. 安装使用

¶ 安装

更新时间: 2025-02-18 09:00:47
编辑

¶ NPM

npm install authing-node-sdk

¶ Yarn

yarn add authing-node-sdk

Github 仓库: https://github.com/Authing/authing-node-sdk (opens new window)

¶ 使用 管理模块

¶ 初始化

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

import { ManagementClient } from "authing-node-sdk";

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

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

完整的参数和释义如下:

  • accessKeyId: Authing 用户池 ID;
  • accessKeySecret: Authing 用户池密钥;
  • timeout: 超时时间,单位为 ms,默认为 10000 ms;
  • host: Authing 服务器地址,默认为 https://api.authing.cn。如果你使用的是 Authing 公有云版本,请忽略此参数。如果你使用的是私有化部署的版本,此参数必填,格式如下: 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",
  });
})();

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

¶ 使用 认证模块

¶ 初始化

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

import { AuthenticationClient } from "authing-node-sdk";

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

完整的参数和释义如下:

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

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

¶ 私有化部署

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

import { ManagementClient } from "authing-node-sdk";

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

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

上一篇: Node.js 下一篇: 用户认证模块
  • 安装
  • 使用 管理模块
  • 使用 认证模块
  • 私有化部署

用户身份管理

集成第三方登录
手机号闪验 (opens new window)
通用登录表单组件
自定义认证流程

企业内部管理

单点登录
多因素认证
权限管理

开发者

开发文档
框架集成
博客 (opens new window)
GitHub (opens new window)
社区用户中心 (opens new window)

公司

400 888 2106
sales@authing.cn
北京市朝阳区北辰世纪中心 B 座 16 层(总)
成都市高新区天府五街 200 号 1 号楼 B 区 4 楼 406 室(分)

京ICP备19051205号

beian京公网安备 11010802035968号

© 北京蒸汽记忆科技有限公司