Skip to main content

Username-Password Login

Using username-password login, you can allow your users to bind a username and log in to CloudBase using the username and password. You can change the username and password, and also check whether the username has been bound.

If the username has not been bound, you need to log in using another method first before you can bind the username. After successful binding, you can log in using the username and password.

Enable Username-Password Login

Go to the CloudBase console, and on the Login Authorization settings page, enable username-password login:

Username Binding Process

Step 1: Initializing the SDK

import cloudbase from "@cloudbase/js-sdk";

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

Step 2: Logging In Using Other Methods

Before binding a username, users need to log in using other methods first, such as email login or WeChat Official Account login, but excluding anonymous login.

The following uses Email Login as an example:

const auth = app.auth();
await auth.signInWithEmailAndPassword(email, password); // Email login
Why must users log in first before binding a username?

A username can be any string that complies with the rules. To prevent malicious actors from registering excessive invalid usernames for your application, CloudBase currently does not allow direct user registration using the username + password method.

Step 3: Binding the Username

When binding a username, you can check whether the username exists in the current cloud development environment, then call the username binding API:

const auth = app.auth();
if (!(await auth.isUsernameRegistered(username))) {
// Check whether the username has been bound.
await auth.currentUser.updateUsername(username); // Bind username
}
Username Rules
  1. Can contain digits and letters, but cannot be purely numeric
  2. Only the symbols - and _ are permitted, and these two symbols are not allowed at the beginning or end.
  3. Length must be between [1, 32]

Login Process

Step 1: Initializing the SDK

import cloudbase from "@cloudbase/js-sdk";

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

Step 2: Log in to CloudBase using username+password

const auth = app.auth();
const loginState = await auth.signInWithUsernameAndPassword(username, password); // Username-password login
Note

Username login and email login use the same password.