Taro.login(option)
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.
Type
(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
Taro.login({
success: function (res) {
if (res.code) {
// Send a network request
Taro.request({
url: 'https://test.com/onLogin',
data: {
code: res.code
}
})
} else {
console.log('Login failed' + res.errMsg)
}
}
})
API Support
API | WeChat Mini-Program | H5 | React Native |
---|---|---|---|
Taro.login | ✔️ |