Skip to main content

User Management

User Types

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

User TypeRoleUser LimitUser InformationRegistrationLogin
Internal UserFixed "Default Internal User Role",
can be assigned custom roles
Limited by plan, refer to Billing DocumentationSaved by system- Go to Cloud Console/Internal User Management to add
- Add via HTTP API
Refer to Login Methods
External Registered UserFixed "Default External User" roleNo user limitSaved by systemVia User RegistrationRefer to Login Methods
Anonymous UserFixed "Default Visitor" roleNo user limitNot saved by systemNo registration requiredRefer to Anonymous Login

Get Current Logged-in User

For auth instance, please refer to SDK Initialization

You can use the auth.currentUser property or the auth.getCurrentUser() method to get the currently logged-in user. This method returns a User instance of the currently logged-in 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",
});

// Get auth instance
const auth = app.auth();

// After login...

// Get user information
const user = auth.currentUser;
// Or
// const user = await auth.getCurrentUser()

Get User Profile

You can get the user's profile information through various properties of the User object:

const user = auth.currentUser;
let uid, name, gender, created_from;

if (user) {
// CloudBase 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 profile updated successfully
});

Refresh User Profile Information

For a multi-platform application, users may update their profile information on one platform, and other platforms may need to refresh the information:

const user = auth.currentUser;

// Refresh user information
user.refresh().then(() => {
// After refresh, the obtained user information is the latest
const { username, gender } = user;
});