Store Extra User Information
System User Table vs Custom User Table
After using CloudBase login authentication, the user's basic information (such as uid, login method, basic profile, etc.) is automatically stored in the system-maintained user table. If you need to store business-related extra user information (such as address, email, membership level, etc.), it's recommended to create a custom user table.
System User Table:
- Automatically maintained by CloudBase
- Stores user authentication-related information (
uid,name,gender, login method, etc.) - Accessed through
auth.currentUser - Not recommended to directly add business fields to this table
Custom User Table (Recommended):
- Created and maintained by developers (e.g.,
userscollection) - Stores business-related extension information (address, email, points, membership level, etc.)
- Associated with system user table through
uid - Flexible field extension to meet business needs
Implementation
1. Create Custom User Table
Create a users collection at CloudBase Platform/Document Database with the following structure:
{
_id: "User record ID",
uid: "CloudBase user ID (associated with system user table)",
address: "User address",
email: "Email",
phone: "Phone number",
vip_level: "Membership level",
createTime: "Creation time"
}
2. Create Extension Information During User Registration
import cloudbase from '@cloudbase/js-sdk';
const app = cloudbase.init({
env: 'your-env-id',
});
const auth = app.auth();
const db = app.database();
// After user registration succeeds
auth.signUp({
phone_number: '+86 13800000000',
verification_code: '123456',
verification_token: 'token',
name: '张三',
}).then(async () => {
// Get current user uid
const user = auth.currentUser;
// Create extension information in custom user table
await db.collection('users').add({
uid: user.uid, // Associate with system user table
address: '北京市朝阳区',
email: 'zhangsan@example.com',
phone: '+86 13800000000',
vip_level: 1,
createTime: new Date(),
});
console.log('User registration and extension information creation successful');
});
3. Query Complete User Information
// Get system user information
const user = auth.currentUser;
console.log('System user information:', user.uid, user.name);
// Get custom extension information
const { data } = await db.collection('users').where({ uid: user.uid }).get();
console.log('User extension information:', data[0].address, data[0].email);
4. Update User Extension Information
// Update information in custom user table
await db.collection('users').where({ uid: auth.currentUser.uid }).update({
address: '上海市浦东新区',
vip_level: 2,
});
Best Practices
- Separation of Concerns: System user table focuses on authentication, custom user table focuses on business logic
- Use uid for Association: Establish association between two tables through the
uidfield - Reasonable Field Design: Design custom user table field structure based on business needs
- Pay Attention to Data Sync: When user information is updated, remember to sync related data tables
Related Documentation
- User Management - User information management and updates
- Database Operations - CloudBase database operation guide
- Best Practices - Best practices for authentication and permission management