title: Taro.login(option)

sidebar_label: login

Gets the login credential (code), which is then used to exchange for the user login status information, including the unique identifier (openid) of the user and the session key (session_key) of the current login. The communication for user data encryption and decryption is performed using the session key. For more usages, see Mini Program Login.

Reference

Type

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

Parameters

Option

Property Type Required Description
timeout number No Timeout time in ms
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
code string The user login credential, which is valid for five minutes. The developer needs to call auth.code2Session in the developer server backend, and exchange the code for information such as the openid and session_key.
errMsg string Call result

Sample Code

  1. Taro.login({
  2. success: function (res) {
  3. if (res.code) {
  4. // Send a network request
  5. Taro.request({
  6. url: 'https://test.com/onLogin',
  7. data: {
  8. code: res.code
  9. }
  10. })
  11. } else {
  12. console.log('Login failed' + res.errMsg)
  13. }
  14. }
  15. })

API Support

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