Class 08: Authorization – Phân quyền
Gán vai trò (role) cho user
Vai trò (role) là gì?
Gán role cho user trong database
// user.entity.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
username: string;
@Column()
password: string;
@Column()
role: string; // Ví dụ: 'admin', 'user', 'moderator'
}Tạo Guard kiểm tra role
Guard trong NestJS
Guard phân quyền theo role
Custom Decorator cho @Roles
Metadata trong NestJS
Tạo decorator @Roles
@RolesSử dụng RolesGuard và @Roles trong Controller
Ví dụ đầy đủ về phần auth có role
Bài tập thực hành
Tổng kết
Last updated