Skip to main content

SMS Verification Code Login

Using SMS verification code login, you can allow users to register and log in to CloudBase using their own phone number, combined with SMS verification code or password.

Version Information
  • Login Authentication (v2) applies to @cloudbase/js-sdk@2.x version
  • If you are using SDK version 1.x, please refer to Login Authentication (v1)
  • v2 version currently does not support Official Account login. If you need to use this method, please use the v1 version

This document will introduce using the V2 version.

Usage Limits and Fees

  • Newly activated pay-as-you-go environments, or those activated before April 9, 2021, enjoy a free quota of 100 items in the first month;
  • For demands exceeding the free quota, developers can go to the CloudBase console to purchase resource packages;
  • SMS delivery is subject to frequency limits:
    • A maximum of 1 message can be sent to the same phone number within 30 seconds;
    • A maximum of 10 messages can be sent to the same phone number within a calendar day;

Preparatory Actions

Enable SMS Verification Code Login

  1. Go to CloudBase/Authentication
  2. In the login methods list, select the SMS Verification Code Login method and click Enable.

Registration Process

When registering with a mobile phone number, you need to first send an SMS verification code and then complete the registration using the verification code.

auth instance, please refer to SDK Initialization

// 1. Send the mobile phone verification code
// Area code required
const phoneNumber = "+86 13800000000";
const verification = await auth.getVerification({
phone_number: phoneNumber
});

// 2. Verify the verification code
// Assume the user-entered verification code "000000" is received here
const verificationCode = "000000";
// Verify the verification code
const verificationTokenRes = await auth.verify({
verification_id: verification.verification_id,
verification_code: verificationCode
});

// 3. If the user already exists, log in
if (verification.is_user) {
await auth.signIn({
username: phoneNumber,
verification_token: verificationTokenRes.verification_token
});
} else {
// 4. Otherwise, register a new user. When registering a new user, you can set the password and username.
// Note: Automatic login occurs after successful signUp.
await auth.signUp({
phone_number: phoneNumber,
verification_code: verificationCode,
verification_token: verificationTokenRes.verification_token,
// Optional: set nickname
name: "Mobile user",
// Optional: set password
password: "password",
// Optional: set login username
username: "username"
});
}
Password Strength Requirements

Password length should be not less than 8 and not greater than 32, and should contain both letters and numbers.

Login Process

Phone Number and Password Login

The Auth.signIn method can be used for phone number, email, username login

  await auth.signIn({
username: phoneNumber,
password: "your-password"
});

Verification Code Login

Auth.signInWithSms method can be used for phone number verification code login

const phoneNum = 'xxx'

// Send the verification code
const verificationInfo = await auth.getVerification({
phone_number: `+86 ${phoneNum}`,
});

// Assume the user-entered verification code "000000" is received here
const verificationCode = "000000";

// Verification Code Login
await auth.signInWithSms({
verificationInfo,
verificationCode: verificationCode,
phoneNum: phoneNum,
});