Skip to main content
Version: 3.x

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.

Reference

Type

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

Parameters

Option

PropertyTypeRequiredDescription
timeoutnumberNoTimeout time in ms
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
codestringThe 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.
errMsgstringCall 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

APIWeChat Mini-ProgramH5React Native
Taro.login✔️