Auth login authentication
getUserInfo
1. Interface Description
Function: Get user information
Interface declaration: getUserInfo(): IGetUserInfoResult
2. Input Parameters
None
3. Response
Field | Type | Required | Description |
---|---|---|---|
openId | string | Required | WeChat openId , empty if not logged in via WeChat authorization |
appId | string | Required | WeChat appId , empty if not logged in via WeChat authorization |
uid | string | Required | User unique ID |
customUserId | string | Required | Developer-defined user unique id, empty if not logged in via custom authentication |
4. Sample Code
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({ env: "xxx" });
const auth = app.auth();
exports.main = async (event, context) => {
const {
openId, // WeChat openId, empty if not logged in via WeChat authorization
appId, // WeChat appId, empty if not logged in via WeChat authorization
uid, // User unique ID
customUserId, // Developer-defined user unique id, empty if not logged in via custom authentication
} = auth.getUserInfo();
console.log(openId, appId, uid, customUserId);
};
getEndUserInfo
1. Interface Description
Function: Get user information
Interface declaration: getEndUserInfo(uid?: string, opts?: ICustomReqOpts): Promise<IGetEndUserInfoResult>
Note
This interface is supported since Node SDK version 2.2.5.
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
uid | string | No | CloudBase user identity. If not passed, user information will be read from environment variables. |
opts | Object | No | Custom request configuration |
3. Response
Field | Type | Required | Description |
---|---|---|---|
userInfo | EndUserInfo | Yes | CloudBase user information |
requestId | string | No | Request unique identifier |
EndUserInfo:
Field | Type | Description |
---|---|---|
openId | string | WeChat openId, empty if not logged in via WeChat authorization |
appId | string | WeChat appId, empty if not logged in via WeChat authorization |
uid | string | User unique ID |
customUserId | string | Developer-defined user unique id, empty if not logged in via custom authentication |
envName | string | CloudBase environment name |
nickName | string | Nickname |
email | string | User login email |
username | string | User login username |
hasPassword | boolean | Whether the user has set a password |
gender | "MALE" \| "FEMALE" \| "UNKNOWN" | CloudBase login username |
country | string | Country (geographical location) |
province | string | Province (geographical location) |
city | string | City (geographical location) |
avatarUrl | string | User avatar URL |
qqMiniOpenId | string | Identifier of the qq Mini Program bound to the user |
4. Sample Code
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({ env: "xxx" });
const auth = app.auth();
exports.main = async (event, context) => {
try {
const { userInfo } = await auth.getEndUserInfo("your user uuid");
console.log(userInfo);
} catch (error) {
console.log(error.message);
}
};
queryUserInfo
1. Interface Description
Function: Get user information
Interface declaration: queryUserInfo(query: IUserInfoQuery, opts?: ICustomReqOpts): Promise<any>
Note
This interface is supported since Node SDK version 2.6.1.
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
query | IUserInfoQuery | No | CloudBase user identity. |
opts | Object | No | Custom request configuration |
IUserInfoQuery
Field | Type | Required | Description |
---|---|---|---|
platform | string | No | Login type. Supported values: PHONE, USERNAME, EMAIL, CUSTOM |
platformId | string | No | Login identifier corresponding to platform: phone number, username, email, or custom login ID |
uid | string | No | User unique ID. If specified, this field takes precedence in the search |
3. Response
Field | Type | Required | Description |
---|---|---|---|
userInfo | EndUserInfo | Yes | CloudBase user information |
requestId | string | No | Request unique identifier |
EndUserInfo:
Field | Type | Description |
---|---|---|
openId | string | WeChat openId, empty if not logged in via WeChat authorization |
appId | string | WeChat appId, empty if not logged in via WeChat authorization |
uid | string | User unique ID |
customUserId | string | Developer-defined user unique id, empty if not logged in via custom authentication |
envName | string | CloudBase environment name |
nickName | string | Nickname |
email | string | User login email |
username | string | User login username |
hasPassword | boolean | Whether the user has set a password |
gender | "MALE" \| "FEMALE" \| "UNKNOWN" | CloudBase login username |
country | string | Country (geographical location) |
province | string | Province (geographical location) |
city | string | City (geographical location) |
avatarUrl | string | User avatar URL |
qqMiniOpenId | string | Identifier of the qq Mini Program bound to the user |
4. Sample Code
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({ env: "xxx" });
const auth = app.auth();
exports.main = async (event, context) => {
try {
const { userInfo } = await auth.getEndUserInfo("your user uuid");
console.log(userInfo);
} catch (error) {
console.log(error.message);
}
};
getClientIP
1. Interface Description
Function: Get client IP
Interface declaration: getClientIP(): string
2. Input Parameters
None
3. Response
Field | Type | Required | Description |
---|---|---|---|
- | string | Required | Client IP |
4. Sample Code
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({ env: "xxx" });
const auth = app.auth();
exports.main = async (event, context) => {
const ip = auth.getClientIP(); // string
console.log(ip);
};
createTicket
1. Interface Description
Function: Get the login credentials ticket for custom login
Interface declaration: createTicket: (customUserId: string, options?: ICreateTicketOpts) => string
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
customUserId | string | Required | Developer-defined user unique id |
ICreateTicketOpts | string | Required | WeChat appId , empty if not logged in via WeChat authorization |
ICreateTicketOpts
Field | Type | Required | Description |
---|---|---|---|
refresh | number | No | access_token refresh time |
expire | number | No | access_token expiration time |
3. Response
Field | Type | Required | Description |
---|---|---|---|
- | string | Required | Custom login credential ticket |
4. Sample Code
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: "xxx",
credentials: require("/path/to/your/tcb_custom_login.json"),
});
const auth = app.auth();
const customUserId = "123456"; // Developer-defined user unique id
const ticket = auth.createTicket(customUserId, {
refresh: 3600 * 1000, // refresh time for access_token
});
console.log(ticket);