[ New ] Cách BaseUser Của Bạn mở rộng 10 chức năng?

Avatar admin | June 8, 2024

Ngoài các chức năng CRUD cơ bản (tạo, đọc, cập nhật và xóa), bạn có thể thêm các chức năng sau đây vào lớp BaseUser để quản lý toàn diện người dùng:

Trước đó cần xem lại bài trước về BaseUser

[ New ] 1 BaseServices CURD API User Đơn Giản Hóa Của Bạn

– Xác thực người dùng: giúp người dùng đăng nhập và đăng xuất.
– Quản lý vai trò và quyền hạn: Cấp cho người dùng vai trò và quyền hạn.
– Khôi phục mật khẩu—Password Recovery—gửi một email yêu cầu đặt lại mật khẩu.
– Xác minh email—Email Verification—Xử lý yêu cầu xác minh email qua email.
– Tìm kiếm người dùng—User Search—có thể tìm kiếm người dùng dựa trên nhiều tiêu chí.
– Quản lý ảnh đại diện (Profile Picture Management): Tải ảnh đại diện của người dùng và cập nhật chúng.
– Lịch sử hoạt động của người dùng, còn được gọi là lịch sử hoạt động của người dùng và

cho phép ghi lại và truy vấn lịch sử hoạt động của người dùng.
– Quản lý trạng thái người dùng bao gồm việc khóa, vô hiệu hóa hoặc kích hoạt tài khoản người dùng.
– Liên kết tài khoản xã hội, còn được gọi là liên kết tài khoản xã hội, cung cấp liên kết và quản lý tài khoản mạng xã hội của người dùng.
– Thông báo người dùng: cung cấp và giám sát thông báo cho người dùng.

BaseUser Implementation
BaseUser với các chức năng của user

Các chức năng mở rộng trong BaseUser

class BaseUser extends BaseService {
    constructor(baseURL) {
        super(baseURL);
    }

    // CRUD Operations
    async createUser(userData) {
        return this.create('/users', userData);
    }

    async getUser(userId) {
        return this.read(`/users/${userId}`);
    }

    async updateUser(userId, userData) {
        return this.update(`/users/${userId}`, userData);
    }

    async deleteUser(userId) {
        return this.delete(`/users/${userId}`);
    }

    async getAllUsers() {
        return this.read('/users');
    }

    // User Authentication
    async login(credentials) {
        return this.create('/auth/login', credentials);
    }

    async logout() {
        return this.create('/auth/logout');
    }

    // Password Recovery
    async requestPasswordReset(email) {
        return this.create('/auth/password-reset-request', { email });
    }

    async resetPassword(token, newPassword) {
        return this.create('/auth/password-reset', { token, newPassword });
    }

    // Email Verification
    async requestEmailVerification(email) {
        return this.create('/auth/email-verification-request', { email });
    }

    async verifyEmail(token) {
        return this.create('/auth/email-verification', { token });
    }

    // Role and Permission Management
    async assignRole(userId, role) {
        return this.create(`/users/${userId}/roles`, { role });
    }

    async getUserRoles(userId) {
        return this.read(`/users/${userId}/roles`);
    }

    // User Search
    async searchUsers(query) {
        return this.read(`/users/search?query=${encodeURIComponent(query)}`);
    }

    // Profile Picture Management
    async uploadProfilePicture(userId, file) {
        const formData = new FormData();
        formData.append('profilePicture', file);
        return this.request(`/users/${userId}/profile-picture`, {
            method: 'POST',
            body: formData
        });
    }

    // User Status Management
    async activateUser(userId) {
        return this.create(`/users/${userId}/activate`);
    }

    async deactivateUser(userId) {
        return this.create(`/users/${userId}/deactivate`);
    }

    async lockUser(userId) {
        return this.create(`/users/${userId}/lock`);
    }

    async unlockUser(userId) {
        return this.create(`/users/${userId}/unlock`);
    }

    // User Notifications
    async getUserNotifications(userId) {
        return this.read(`/users/${userId}/notifications`);
    }

    async markNotificationAsRead(userId, notificationId) {
        return this.create(`/users/${userId}/notifications/${notificationId}/read`);
    }
}

// Sử dụng lớp BaseUser với các chức năng mở rộng
const baseUser = new BaseUser('https://api.example.com');

// Ví dụ: Đăng nhập người dùng
baseUser.login({ email: '[email protected]', password: 'password123' })
    .then(response => console.log('Logged in:', response))
    .catch(error => console.error('Login error:', error));

// Ví dụ: Khôi phục mật khẩu
baseUser.requestPasswordReset('[email protected]')
    .then(response => console.log('Password reset requested:', response))
    .catch(error => console.error('Password reset error:', error));

// Ví dụ: Gán vai trò cho người dùng
baseUser.assignRole(1, 'admin')
    .then(response => console.log('Role assigned:', response))
    .catch(error => console.error('Assign role error:', error));

Giải thích về các chức năng mới về BaseUser

– Xác thực người dùng: cung cấp cách đăng nhập và xác thực.
– Khôi phục mật khẩu: Có hai cách để yêu cầu PasswordReset và resetPassword.
– Xác minh email bao gồm các phương thức yêu cầu xác minh email và chứng minh email.
– Quản lý vai trò: Các phương thức assignRole và getUserRoles được cung cấp.
– Tìm kiếm người dùng: bao gồm cách tìm kiếm người dùng.
– Quản lý ảnh đại diện: cung cấp cách gửi ProfilePicture.
– Quản lý trạng thái người dùng: bao gồm các phương thức khởi động người dùng và

deactivate người dùng, khóa người dùng và mở người dùng
– Thông báo người dùng: cung cấp các phương thức để nhận thông báo người dùng và đánh dấu thông báo như đọc.

Với các chức năng mở rộng này, lớp BaseUser sẽ có thể đáp ứng được hầu hết các yêu cầu quản lý người dùng trong các ứng dụng phức tạp.

Xem thêm:

Viblo authorization


Written by admin


Comments

This post currently has no responses.

Leave a Reply