title: Taro.getUserInfo(option)

sidebar_label: getUserInfo

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

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

Parameters

Option

Property Type Required Description
lang "en" | "zh_CN" | "zh_TW" No The language of the displayed user information
withCredentials boolean No Indicates 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) => void No The callback function used when the API call completed (always executed whether the call succeeds or fails)
fail (res: any) => void No The callback function for a failed API call
success (res: Result) => void No The callback function for a successful API call

SuccessCallbackResult

Property Type Description
cloudID string The 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.
encryptedData string The complete encrypted user data, including the sensitive data. For details, see signature, verification, encryption, and decryption of user data.
iv string The initial vector of the encryption algorithm. For details, see signature, verification, encryption, and decryption of user data.
rawData string Raw data string that excludes sensitive information and is used to calculate signatures.
signature string The 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.
userInfo UserInfo User information object, excluding openid and other sensitive information.
errMsg string Call result

Sample Code

  1. // It must be called after user authorization is obtained.
  2. Taro.getUserInfo({
  3. success: function(res) {
  4. var userInfo = res.userInfo
  5. var nickName = userInfo.nickName
  6. var avatarUrl = userInfo.avatarUrl
  7. var gender = userInfo.gender // Gender: 0 - Unknown; 1 - Male; 2 - Female
  8. var province = userInfo.province
  9. var city = userInfo.city
  10. var country = userInfo.country
  11. }
  12. })

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:

  1. {
  2. "openId": "OPENID",
  3. "nickName": "NICKNAME",
  4. "gender": GENDER,
  5. "city": "CITY",
  6. "province": "PROVINCE",
  7. "country": "COUNTRY",
  8. "avatarUrl": "AVATARURL",
  9. "unionId": "UNIONID",
  10. "watermark": {
  11. "appid":"APPID",
  12. "timestamp": TIMESTAMP
  13. }
  14. }

API Support

API WeChat Mini-Program H5 React Native
Taro.getUserInfo ✔️