Account Association
Each Cloud Development user account can associate additional login methods besides the one used during initial registration. Once associated, users can log in to the same account using any of these methods.
Example:
- Users using custom login can associate WeChat login; after association, they can log in using either method.
- Users using email login can associate username-password login; after association, they can log in using either method.
- Users using anonymous login can associate custom login and WeChat login; after association, the account will be converted to an official user and can log in to Cloud Development using any method.
Linking WeChat Login
Steps to link WeChat login:
The user logs in to CloudBase using any login method except WeChat login
Get Provider:
const auth = app.auth();
const provider = auth.weixinAuthProvider({
appid: "....",
scope: "snsapi_base"
});
- Redirect to the provider's page for login:
auth.currentUser.linkWithRedirect(provider);
- After the user logs in on the WeChat page, they will be redirected back to your page. You can then call
Provider.getLinkRedirectResult()
during page load to obtain the association result:
const provider = auth.weixinAuthProvider();
provider.getLinkRedirectResult().then((result) => {
// Associated successfully
});
Linking Custom Login
Steps to link custom login:
- The user logs in to CloudBase using any login method except custom login;
- Use
User.linkWithTicket
to obtain a custom login Ticket, then link the custom user:
const auth = app.auth();
const ticket = "......"; // custom login ticket
auth.currentUser.linkWithTicket(ticket).then((result) => {
// Associated successfully
});
Linking Email-Password Login
Steps to link email-password login:
- The user logs in to CloudBase using any login method
- Update the user's password:
const auth = app.auth();
auth.currentUser.updatePassword(password).then(() => {
// Password set successfully
});
- Update the user's email. After the user clicks the verification email, the association is successful:
auth.currentUser.updateEmail(email).then(() => {
// Verification email sent successfully
});
Linking Username-Password Login
Steps to link email-password login:
- The user logs in to CloudBase using any login method except anonymous login:
// Take email login as an example
await app.auth().signInWithEmailAndPassword(email, password);
- Bind the login username:
await app.auth().currentUser.updateUsername(username); // Bind username
- After successful binding, you can log in with username and password:
const loginState = await app.auth().signInWithUsernameAndPassword(username, password); // Username-password login
CloudBase supports associating WeChat UnionID. If developers have multiple WeChat Open Platform or Public Platform applications, they can associate WeChat UnionID to uniquely identify users.
For more information about UnionID, please refer to the WeChat official documentation
Login with UnionID
When using WeChat to log in to CloudBase, you can include the UnionID by using Provider.sign({ withUnionId: true })
:
auth.weixinAuthProvider().signIn({ withUnionId: true })
If the user logs in to CloudBase with UnionID, then:
- If a WeChat account associated with this UnionID has previously logged in to CloudBase
- If a primary account is set, it will log in to CloudBase as this account
- If no primary account is set, it will log in to CloudBase with the current WeChat account
- If not, a new CloudBase user will be registered
Retrieve the WeChat account corresponding to the UnionID
You can obtain the Uid account(s) corresponding to the UnionId (which may have one or multiple) via User.getLinkedUidList()
:
auth.currentUser.getLinkedUidList().then(list => {
const {
users, // CloudBase accounts corresponding to the UnionID
hasPrimaryUid // whether it contains a primary account
} = list
})
After obtaining the list, you can ask the user or automatically select one of the accounts as the primary account for the user.
Set Primary Account
You can set the primary account corresponding to the UnionId via User.setPrimaryUid()
:
auth.currentUser.setPrimaryUid(uid).then(() => {
// Primary account set successfully
})
After being set as the primary account, logging in with the UnionID will always access this primary account. -->