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.

Associate Phone Number and Password Login

Note

is only supported in the Shanghai region

When the current user supports password login, you can associate a phone number for the user. After association, the user can log in using "phone number + password".

  1. The user logs in using any login method.
  2. Obtain sudo_token. Here, the sudo_token is obtained via password authentication. Alternatively, you can use email verification codes, phone verification codes, etc. For details, see 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 a 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 verification code entered by the user.
// 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 mobile phone number.
await auth.bindPhoneNumber({
sudo_token: sudo_token,
phone_number: phoneNumber,
verification_token: verification_token,
});

Associate Email Password Login

When the current user supports password login, you can bind an email address to the user. After binding, users can log in using "Email + Password":

  1. Users log in to TCB using any login method.
  2. Obtain sudo_token. Here, the sudo_token is obtained via password authentication. Alternatively, you can use email verification codes, phone verification codes, etc. For details, see 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.
// 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 verification code entered by the user.
// 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.
await auth.bindEmail({
sudo_token: sudo_token,
email: email,
verification_token: verification_token,
});

Association with WeChat Login

The steps to associate with WeChat login are as follows:

  1. Users log in to TCB using any login method (except WeChat login).

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

  3. Use the authorization token to associate with WeChat login.

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

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