¶ 可用的 Node Modules
Update Time: 2025-02-18 09:00:47
目前 Authing Pipeline 中可以 Use 以下 Node Modules:
- Authing SDK for Node.js (opens new window)
- 网络请求库 axios (opens new window)
- lodash
- Authing 内置工具集函数 utils
¶ Authing SDK for Node.js
出于安全考虑, Authing 会通过特殊方式,Use 你的用户池 ID(userPoolId) 和用户池密钥(secret) 初始化 authing-js-sdk,此过程不会将你的用户池密钥发送到公网。你可以 Use 全局变量 authing,请勿再次初始化 SDK!
开发者可以直接 Use 初始化过后的 authing 实例,无需手动初始化!Authing Pipeline 会自动帮助开发者 take care 初始化过程。
如下所示:
async function pipe(user, context, callback) {
if (!user.email.endsWith("@authing.cn")) {
return callback(null, user, context);
}
try {
await authing.roles.addUsers("ROLE", [user.id]);
} catch (error) {}
callback(null, user, context);
}
解释一下:
- 2-4 行判断用户邮箱是否已
@authing.cn
结尾,如果不是,可以直接跳过此 Pipeline 函数。 - 6-11 行调用 SDK 的角色管理 SDK API,授权用户角色
ROLE
。- 在这里我们 Use 了 env.ROOT_GROUP_ID 通过环境变量来获取组 ID,这样可以避免硬编码。关于如何在 Pipelien 函数中 Use 环境变量,请见Use 环境变量。
- 13 行调用回调函数 callback,第一个参数为 null,表示没有错误抛出,可以继续执行下面的认证流程。关于如何 Use callback 以及 Pipelien 函数的完整 API,请见 Pipeline 函数 API 文档。
¶ 网络请求库
目前 Authing 支持 Use axios
,且支持 async/await 语法 🚀!
axios 详细文档请移步其官方文档 (opens new window)。
¶ lodash
需要开发者手动导入:
const _ = require("lodash");
详细文档请移步其官方文档 (opens new window)。
¶ 内置工具集 utils
Authing 内置封装了一些实用的函数,供开发者直接调用。
需要开发者手动导入:
const utils = require("./utils");
¶ 检查 IP 是否位于 IP 段内
Use 方法:
utils.ipRangeCheck(IP, [start, end]);
返回值为 boolean。
示例:以下 Pipeline 函数实现注册 IP 段白名单功能。
async function pipe(context, callback) {
const utils = require("./utils");
const ip = context.ip;
if (ip && utils.ipRangeCheck(ip, ["110.53.254.1", "110.53.254.255"])) {
return callback(null, context);
}
return callback(new Error("Access Denied!"));
}
¶ 其他 Node 自带 Module
Authing Pipeline Use node8 引擎,node8 的所有内置模块 (opens new window)均可 Use ,如 querystring
等。