User Management
Creating a User
Developers can call the following login methods to sign in or create a user:
- Username and Password Login
- SMS Verification Code Login
- Email Login
- WeChat Authorized Login
- Custom Login
User Types
Users are categorized into internal users, external registered users, and anonymous users:
| User Types | Role | User Count Limit | User Information |
|---|---|---|---|
| Internal Users | Fixed "Default Internal User Role", can assign custom roles | Subject to plan restrictions, refer to Billing Documentation | Saved by the system |
| External Registered Users | Fixed "Default External User Role" | Unlimited | Saved by the system |
| Anonymous Users | Fixed "Default Visitor Role" | Unlimited | Not saved by the system |
Obtain Current Logged-in User
auth instance please refer to SDK Initialization
You can use the auth.currentUser property or the auth.getCurrentUser() method to obtain the currently logged-in user. This method returns the User instance of the current user. If the user is not logged in, it returns null:
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id"
});
// Obtain the auth instance
const auth = app.auth();
// After logging in...
// Obtain user information
const user = auth.currentUser
// Or
// const user = await auth.getCurrentUser()
Obtain User Profile
You can access various properties of the User object to obtain the user's profile information:
const user = auth.currentUser
let uid, name, gender, created_from
if (user) {
// Cloud Base unique user ID
uid = user.uid
// Nickname
name = user.name
// Gender
gender = user.gender
// Creation source
created_from = user.created_from;
}
Update User Profile
You can use the User.update method to update the user's profile information. For example:
const user = auth.currentUser
user
.update({
name: "Tony Stark",
gender: "MALE"
})
.then(() => {
// User information is updated successfully.
});
Refresh User Profile Information
For a multi-device application, users may update their personal information on one device, and other devices may need to refresh the information:
const user = auth.currentUser;
// Refresh user information.
user.refresh().then(() => {
// After refreshing, the obtained user information will be the latest.
const {
username,
gender
} = user;
});