Account Association
Each Cloud Base user account can be associated with other login methods in addition to the initial login method. After association, users can log in to the same Cloud Base account using any associated login method.
Linking WeChat Login
Steps to link WeChat login:
The user logs in to CloudBase using any login method except WeChat login
Refer to WeChat Authorization Login to obtain the WeChat authorization
provider_token
Use the authorization token to link WeChat login
const app = cloudbase.init({
env: "xxxx-yyy"
});
const provider_token = "test_provider_token"; // Authorization token obtained in the previous step
const auth = app.auth();
await auth.bindWithProvider({
provider_token
});
Linking Email-Password Login
If the current user supports password login, the following method can be used to bind an email to the current user. After binding, the user can log in using email + password.
- The user logs in to CloudBase using any login method
- Obtain
sudo_token
. Here,sudo_token
can be obtained using password authentication. Other methods such as email verification codes or phone verification codes are also available. For details, refer to the Auth.sudo interface.
const auth = app.auth();
// Assume the user input password is passwd.
const password = "passwd";
// Obtain sudo_token. The expiry date of sudo_token defaults to 10 minutes.
const sudo_token = await auth.sudo({
password: password
});
- Send a verification code to the email
// Assume the user email is test@example.com.
const email = "test@example.com";
// Obtain email Captcha.
const verification = await auth.getVerification({
email: email
});
- Verify the user-input Captcha.
// Assume the user enters the Captcha 000000.
const verificationCode = "000000";
// Verify the Captcha.
const verificationTokenRes = await auth.verify({
verification_id: verification.verification_id,
verification_code: verificationCode
});
const verification_token = verificationTokenRes.verification_token
- Use verification_token and sudo_token to bind the email.
await auth.bindEmail({
sudo_token: sudo_token,
email: email,
verification_token: verification_token
});
Link Phone Number for Password Login
Assume the current user supports password login. The following method can bind a mobile number to the user. After binding, the user can log in using the mobile number + password.
- The user logs in to CloudBase using any login method
- Obtain sudo_token. Here, sudo token is obtained using the password method. You can also use methods such as email verification code and phone verification code. For details, refer to the Auth.sudo interface.
const auth = app.auth();
// Assume the user input password is passwd.
const password = "passwd";
// Obtain sudo_token. The expiry date of sudo_token defaults to 10 minutes.
const sudo_token = await auth.sudo({
password: password
});
- Send a verification SMS to the user's mobile phone.
const auth = app.auth();
// Assume the user's mobile phone number is 13800000000.
const phoneNumber = "+86 13800000000";
// Get a Captcha.
const verification = await auth.getVerification({
phone_number: phoneNumber
});
- Verify the user-input Captcha.
// Assume the user enters the Captcha 000000.
const verificationCode = "000000";
// Verify the Captcha.
const verificationTokenRes = await auth.verify({
verification_id: verification.verification_id,
verification_code: verificationCode
});
const verification_token = verificationTokenRes.verification_token
- Use verification_token and sudo_token to bind the mobile phone number.
await auth.bindPhoneNumber({
sudo_token: sudo_token,
phone_number: phoneNumber,
verification_token: verification_token
});
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. -->