Overview
The@CrudAuth decorator is a class decorator that adds authentication and authorization capabilities to your CRUD controller. It allows you to filter queries based on user context, persist user data in create/update operations, and control serialization based on user permissions.
Signature
Parameters
Authentication and authorization configuration
The property name where the authenticated user is stored in the request object (e.g.,
req.user)Function that returns filter conditions based on the request. These conditions are applied to all read operations using AND logic.Parameters:
req- The request object containing the authenticated user
Function that returns filter conditions to be applied using OR logic. Useful for scenarios where a user can access their own resources OR shared resources.Parameters:
req- The request object containing the authenticated user
Function that returns data to be persisted in create and update operations. Commonly used to set the owner/creator of a resource.Parameters:
req- The request object containing the authenticated user
Function that returns class-transformer options for serialization based on the request context.Parameters:
req- The request object containing the authenticated user
Function that returns serialization groups based on the request context. These groups are used by class-transformer to include/exclude fields.Parameters:
req- The request object containing the authenticated user
Usage
Basic Authentication Filter
Persist User Data on Create
userId and email are automatically added to the entity.
Using OR Conditions
Role-Based Serialization
Complete Example
Integration Test Example
From the source tests:packages/crud/test/crud-request.interceptor.spec.ts:62-86
Implementation Details
The decorator stores auth options in metadata:packages/crud/src/decorators/crud-auth.decorator.ts:4-8
The
@CrudAuth decorator should be used in combination with @Crud. Apply both decorators to the controller class, with @Crud typically placed first.See Also
- @Crud - Main CRUD decorator
- @Override - Override routes with custom logic
- @ParsedRequest - Access request data in overridden routes