WeChat authorization login
Applications authorized by the WeChat open platform (common website and mobile applications) can use WeChat login with CloudBase directly.
Prerequisites
Enable WeChat login
- First, you need a registered account on the open platform. If not, please visit WeChat Open Platform to apply.
- Go to Cloud Development Platform/Identity Verification/Login Methods
- In the login methods list, select "WeChat open platform login" method, click "go to settings" to fill in
AppIdandAppSecret.
Login Process
Get the authorization page address of a third-party platform
Fill in WeChat platform ID, redirect URI, and custom status flag fields to identify the callback source.
Auth.genProviderRedirectUri is used to generate third-party platform authorization page URL
const { uri } = await auth.genProviderRedirectUri({
provider_id: "wx_open", // WeChat open platform
provider_redirect_uri: providerUri, // redirect uri
state: state, // custom status flag
other_params: otherParams, // other parameters
});
Access the URI and authorize, then callback to the designated address to obtain the third-party platform Token
Redirect the user to the URI, such as location.href = uri. After user authorization is complete, callback to the designated provider_redirect_uri address.
At this point, the URL query carries parameters such as the authorization code and state.
Check if the state meets expectations (such as the set wx_open)
2. Retrieve the code parameter carried in the URL param when redirected back to the webpage from a third-party platform
const provider_code = "your_provider_code";
3. If it behaves as expected, obtain the third-party platform Token
const { provider_token } = await auth.grantProviderToken({
provider_id: "wx_open",
provider_redirect_uri: "curpage", // Specify the URL address to jump back to the third-party platform
provider_code: provider_code, // The code parameter carried in the URL param when redirected back to the webpage from a third-party platform
});
Log in via third-party platform Token
Auth.signInWithProvider method is used for third-party platform login
await auth.signInWithProvider({
provider_token: provider_token,
});
sign-up process
When using WeChat authorization login, developers need to use Auth.bindWithProvider to create user information for first-time WeChat login users.
The specific process is as follows:
- When a user logs in with WeChat for the first time, calling signInWithProvider will return the
not_founderror code. - Register a user using other methods (such as mobile captcha, username/password, or mailbox verification code), then call Auth.bindWithProvider to synchronize the user's WeChat information (such as Nickname, avatar, etc.) to the CloudBase user profile. Subsequently, logging in with the same WeChat account will directly log in to the corresponding CloudBase user.
- Let the user trigger the login process again.
try {
await auth.signInWithProvider({
provider_token: provider_token,
});
} catch (e) {
// Unassociated, go to associate user
if (e.error === "not_found") {
// Use other methods to register user
// Bind user
await auth.bindWithProvider({
provider_token: provider_token,
});
// Binding succeeded, have the user log in again
} else {
throw e;
}
}