Skip to main content

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.

Mobile Number and Password Login Association

When the current user supports password login, you can associate a mobile number for the user. After association, the user can log in using "Mobile Number + Password":

  1. The user logs in using any login method.
  2. Obtain the sudo_token. Here, the sudo_token is obtained via the password method. Alternatively, you can use methods such as email verification codes or mobile verification codes. For details, refer to the Auth.sudo interface.
import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
env: "your-env-id"
});

// Obtain the auth instance
const auth = app.auth();

// Assume the user input password is passwd.
const password = "passwd";

// Obtain sudo_token. The expiration time of sudo_token defaults to 10 minutes.
const sudo_token = await auth.sudo({
password: password
});
  1. Send the verification SMS to the user's mobile phone.
// Assume the user's mobile phone number is 13800000000.
const phoneNumber = "+86 13800000000";

// Send the verification code
const verification = await auth.getVerification({
phone_number: phoneNumber
});
  1. Verify the user-entered verification code.
// 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;
  1. Bind the mobile phone number using verification_token and sudo_token.
await auth.bindPhoneNumber({
sudo_token: sudo_token,
phone_number: phoneNumber,
verification_token: verification_token
});

Associate Email Password Login

If the current user supports password login, you can associate an email address for the user. After association, the user can log in using "Email + Password":

  1. The user logs in to CloudBase using any login method.
  2. Obtain the sudo_token. Here, the sudo_token is obtained via the password method. Alternatively, you can use methods such as email verification codes or mobile verification codes. For details, refer to the Auth.sudo interface.
// Assume the user input password is passwd.
const password = "passwd";

// Obtain sudo_token. The expiration time of sudo_token defaults to 10 minutes.
const sudo_token = await auth.sudo({
password: password
});
  1. Send the verification code to the email address.
// Assume the user email is test@example.com.
const email = "test@example.com";

// Obtain email Captcha.
const verification = await auth.getVerification({
email: email
});
  1. Verify the user-entered verification code.
// 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;
  1. Use verification_token and sudo_token to bind the email address.
await auth.bindEmail({
sudo_token: sudo_token,
email: email,
verification_token: verification_token
});

Associate WeChat Login

Associate WeChat Login by following these steps:

  1. The user logs in to CloudBase using any login method except WeChat login.

  2. Refer to WeChat Authorized Login to obtain the WeChat authorization provider_token

  3. Use the authorization token to associate WeChat login.

const app = cloudbase.init({
env: "xxxx-yyy"
});
const provider_token = "test_provider_token"; // the authorization token obtained in the previous step

const auth = app.auth();
await auth.bindWithProvider({
provider_token
});