# wx.login(Object object)

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.

# Parameters

# Object object

Property Type Default Value Required Description Minimum Version
timeout number No Timeout time in ms 1.9.90
success function No The callback function for a successful API call
fail function No The callback function for a failed API call
complete function No The callback function used when the API call completed (always executed whether the call succeeds or fails)

# object.success callback function

# Parameters
# Object res
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.

# Sample Code

wx.login({
  success (res) {
    if (res.code) {
      // Send a network request
      wx.request({
        url: 'https://test.com/onLogin',
        data: {
          code: res.code
        }
      })
    } else {
      console.log('Login failed' + res.errMsg)
    }
  }
})
点击咨询小助手