WeChat Authorization Login
Applications authorized by the WeChat Open Platform (such as regular website applications and mobile applications) can directly use WeChat to log in to CloudBase.
Prerequisites
Enable WeChat Login
- You need a registered account on the Open Platform. If you don't have one, go to WeChat Open Platform to apply.
- Go to Cloud Development Platform/Authentication/Login Methods
- In the login methods list, select "WeChat Open Platform Login", click "Go to Settings", and fill in
AppIdandAppSecret
Sign-up Flow
When using WeChat authorization login, a corresponding CloudBase user account will be automatically created for users during their first WeChat login, eliminating the need for a separate registration process.
The specific process is as follows:
- When a user logs in with WeChat for the first time, the system will automatically create a CloudBase user account.
- The user's WeChat information (such as nickname, avatar, etc.) will be automatically synchronized to the CloudBase user profile.
- When using the same WeChat account to log in subsequently, you will be directly logged into the corresponding CloudBase user.
Login Flow
Obtain the third-party platform authorization page URL
Fill in the WeChat Platform ID, Redirect URI, and Custom State Identifier fields to identify the source of platform callbacks.
Auth.genProviderRedirectUri is used to generate the third-party platform authorization page URL
const {
uri
} = await auth.genProviderRedirectUri({
provider_id: provider_id: "wx_open", // WeChat Open Platform
provider_redirect_uri: provider_redirect_uri: providerUri, // Redirect URI
state: state: state, // Custom State Identifier
other_params: other_params: otherParams // Other parameters
});
After accessing the URI and granting authorization, it will callback to the specified address and obtain the third-party platform Token
Redirect the user to the URI, for example location.href = uri. After the user grants authorization, it will callback to the specified provider_redirect_uri address.
// At this point, the URL query contains parameters such as authorization code, state, etc.
// 1. Check whether the state matches the expected value (e.g., wx_open)
// 2. Obtain the code parameter contained in the URL parameters when the third-party platform redirects back to the page.
const provider_code = "your_provider_code";
// 3. If it meets expectations, obtain the third-party platform Token
const {
provider_token
} = await auth.grantProviderToken({
provider_id: "wx_open",
provider_redirect_uri: provider_redirect_uri: "curpage", // Specify the redirect URL for the third-party platform
provider_code: provider_code: provider_code // The code parameter contained in the URL parameters when the third-party platform redirects back to the page
});
Login via Third-party Platform Token
Auth.signInWithProvider method is used for third-party platform login
await auth.signInWithProvider({
provider_token: provider_token
});