# Available Node Modules
- Authing SDK for Node.js (opens new window)
- Network request library axios (opens new window)
- lodash
- Authing built-in toolset function utils
# Authing SDK for Node.js
For security reasons, Authing will use your user pool ID (userPoolId) and user pool key (secret) to initialize authing-js-sdk in a special way. This process will not send your user pool key to the public network. You can use the global variable authing, **Do not initialize the SDK again! **
Developers can directly use initialize after the authing instance, no need to initialize manually! Authing Pipeline will automatically help developers take care of the initialization process.
As follows:
async function pipe(user, context, callback) {
if (!user.email.endsWith('@authing.cn')) {
return callback(null, user, context)
}
try {
await authing.authz.addUserToGroup({
userId: user._id,
groupId: env.ROOT_GROUP_ID
})
} catch (error) {}
callback(null, user, context)
}
explain:
- Lines 2-4 determine whether the user mailbox has ended with
@authing.cn
, if not, you can skip this Pipeline function directly. - Lines 6-11 call Authing's Role-Based Access Control (RBAC) API to add the requesting user to the user group
ROOT_GROUP_ID
.- Here we use env.ROOT_GROUP_ID to get the group ID through environment variables, which can avoid hard coding. For how to use environment variables in Pipelien functions, please see Using Environment Variables.
- The callback function is called on line 13, and the first parameter is null, which means that no error is thrown, and the following authentication process can be continued. For how to use callback and the complete API of Pipelien function, please refer to Pipeline Function API Document.
# Network request library
Currently Authing supports the use of axios**, and supports async/await syntax đ!
For detailed axios documentation, please go to its official documentation (opens new window).
# lodash
Developers need to manually import:
const _ = require("lodash")
For detailed documentation, please go to its official documentation (opens new window).
# Built-in toolset utils
Authing encapsulates some useful functions for developers to call directly.
Developers need to manually import:
const utils = require("./utils")
# Check if the IP is in the IP range
Instructions:
utils.ipRangeCheck(IP, [start, end])
The return value is boolean.
Example: The following Pipeline function implements the whitelist function of registering IP segments.
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!'))
}
# Other Node comes with Module
Authing Pipeline uses node8 engine, all built-in modules of node8 (opens new window) can be used, such as querystring
etc.