Guards System Overview
Declarative Permissions
@api guards optionSimple permission setup
Guard Types
admin, user, queryType definitions provided
Custom Guards
Business logicFlexible extension
Hierarchical Permissions
Role-based controlRBAC implementation
Understanding Guards
What are Guards?
Guards are functions that verify permissions before an API method executes. They are used in the following situations:- APIs accessible only to logged-in users
- APIs accessible only to administrators
- APIs accessible only to users meeting specific conditions
- Declarative: Simply declare like
@api({ guards: ["admin"] }) - Reusable: Use the same Guards across multiple APIs
- Testable: Test Guards independently
- Separation of Concerns: Permission logic separated from business logic
Guards Execution Flow
Guard Types
Sonamu has three Guard types (user, admin, query) defined. These are type-safe keys, and the actual verification logic must be implemented in guardHandler in sonamu.config.ts.
user Guard
user Guard allows only logged-in users to access. It’s the most basic authentication Guard.
- All APIs that require login
- APIs handling user-specific data (my profile, my orders, etc.)
- Content creation/modification/deletion
admin Guard
admin Guard allows only administrators to access. Used for sensitive administrative functions.
- System settings changes
- Accessing all user data
- User management (activation/deactivation, permission changes)
- Statistics and analytics data
- Sensitive business logic
query Guard
query Guard implements simple authentication via query string parameters. Mainly used for public APIs or temporary links.
- Temporary download links sent via email
- Shareable public APIs
- Webhook callbacks (API key verification)
- Cases requiring simple authentication
The specific implementation of
query Guard may vary by project. It can be extended with various
methods like API keys, tokens, signatures, etc.Combining Multiple Guards
You can apply multiple Guards to a single API. All Guards must pass for the API to execute.Implementing Guards
Guards are implemented inguardHandler in sonamu.config.ts. Sonamu automatically executes guardHandler for each API call.
Implementing guardHandler
Adding Custom Guard Types
To add project-specific Guards, extend theGuardKeys interface.
guardHandler.
Dynamic Verification
When you need to verify permissions dynamically based on API parameters, you can use theapi parameter in guardHandler.
Role-Based Access Control (RBAC)
Use RBAC (Role-Based Access Control) for more complex permission systems.Defining Roles and Permissions
Implementing Permission-Based Guards
To integrate the RBAC system with Guards, verify permissions inguardHandler.
Using Permission Guard
Resource-Based Permission Control
Pattern for verifying ownership of specific resources.Testing Guards
You can test Guards independently.Cautions
Next Steps
Authentication Setup
Building authentication system
Session Management
Session and token management
Context
Learn more about Context
@api Decorator
API decorator options