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 also change password or reset password.
- 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.
If the username has not been bound, you need to log in using another method first before binding the username. After successful binding, you can log in using the username and password.
Preparatory Actions
Enable Username-Password Login
- Go to CloudBase/Authentication
- In the login methods list, select the Username-Password Login method and click Enable.
Registration Process
The registration for username login is unique: it requires first using another method to complete registration, and then setting the username during the registration process.
Currently supports mobile phone verification code, email verification code registration.
Register using another method and set username.
Taking Email Login as an example, set the username during new user registration:
auth instance, please refer to SDK Initialization
const email = "test@example.com";
auth.signUp({
email: email,
// Refer to the Captcha and verification token obtained during email login.
verification_code: verificationCode,
verification_token: verificationTokenRes.verification_token,
// Set username.
username: "userFromEmail",
// Optional, set password.
password: "password123"
});
- Can contain digits and letters, but cannot be purely numeric
- Only the symbols
-
and_
are permitted, and these two symbols are not allowed at the beginning or end. - Length must be between
[1, 32]
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.
Login Flow
After obtaining the account credentials at the login-required location in your project, execute the following code:
const loginState = await auth.signIn({
username: "your username",
password: "your password"
});
The passwords for username login and email login are the same.