Skip to main content
Version: Next

Taro.getUserInfo(option)

Gets user information.

API Adjustment Description If this API is called after user authorization is obtained, the user information is returned. If this API is called without user authorization, the authorization pop-up window will not appear, and the fail callback is directly executed. (For details, see Announcement).

Reference

Type

(option?: Option) => Promise<SuccessCallbackResult>

Parameters

Option

PropertyTypeRequiredDescription
lang"en" | "zh_CN" | "zh_TW"NoThe language of the displayed user information
withCredentialsbooleanNoIndicates whether to include login status information. When withCredentials is true, Taro.login must be called previously and the login status must be effective. In this case, sensitive information such as encryptedData and iv is returned. When withCredentials is false, the login status is not required, and sensitive information such as encryptedData and iv is not returned.
complete(res: any) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: any) => voidNoThe callback function for a failed API call
success(res: Result) => voidNoThe callback function for a successful API call

SuccessCallbackResult

PropertyTypeDescription
cloudIDstringThe Cloud ID corresponding to sensitive data. It is returned only in Mini Programs for which Cloud Base is enabled. The open data can be directly obtained via cloud call. See details.
encryptedDatastringThe complete encrypted user data, including the sensitive data. For details, see signature, verification, encryption, and decryption of user data.
ivstringThe initial vector of the encryption algorithm. For details, see signature, verification, encryption, and decryption of user data.
rawDatastringRaw data string that excludes sensitive information and is used to calculate signatures.
signaturestringThe string generated with SHA-1 (rawData + sessionkey), which is used to verify the user information. For details, see signature, verification, encryption, and decryption of user data.
userInfoUserInfoUser information object, excluding openid and other sensitive information.
errMsgstringCall result

Sample Code

// It must be called after user authorization is obtained.
Taro.getUserInfo({
success: function(res) {
var userInfo = res.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var gender = userInfo.gender // Gender: 0 - Unknown; 1 - Male; 2 - Female
var province = userInfo.province
var city = userInfo.city
var country = userInfo.country
}
})

There are two ways to get sensitive data. One is to use the Decryption Algorithm for Encrypted Data. The acquired open data has the following json structure:

{
"openId": "OPENID",
"nickName": "NICKNAME",
"gender": GENDER,
"city": "CITY",
"province": "PROVINCE",
"country": "COUNTRY",
"avatarUrl": "AVATARURL",
"unionId": "UNIONID",
"watermark": {
"appid":"APPID",
"timestamp": TIMESTAMP
}
}

API Support

APIWeChat Mini-ProgramH5React Native
Taro.getUserInfo✔️