Skip to main content

User Management

Creating a User

Developers can call the following login methods to sign in or create a user:

User Types

Users are categorized into internal users, external registered users, and anonymous users:

User TypesRoleUser Count LimitUser Information
Internal UsersFixed "Default Internal User Role", can assign custom rolesSubject to plan restrictions, refer to Billing DocumentationSaved by the system
External Registered UsersFixed "Default External User Role"UnlimitedSaved by the system
Anonymous UsersFixed "Default Visitor Role"UnlimitedNot 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;
});