Skip to main content
Learn how to implement role-based access control for API endpoints using Sonamu’s Guards system.

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
Advantages of Guards:
  1. Declarative: Simply declare like @api({ guards: ["admin"] })
  2. Reusable: Use the same Guards across multiple APIs
  3. Testable: Test Guards independently
  4. 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.
When to use?
  • 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.
When to use?
  • 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.
When to use?
  • 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 in guardHandler in sonamu.config.ts. Sonamu automatically executes guardHandler for each API call.

Implementing guardHandler

Adding Custom Guard Types

To add project-specific Guards, extend the GuardKeys interface.
Now implement new Guards in guardHandler.

Dynamic Verification

When you need to verify permissions dynamically based on API parameters, you can use the api parameter in guardHandler.
Note: For permission verification requiring complex business logic or database queries, it’s better to handle it inside API methods rather than in Guards. Guards should only handle simple authentication/authorization checks.

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 in guardHandler.

Using Permission Guard

Resource-Based Permission Control

Pattern for verifying ownership of specific resources.

Testing Guards

You can test Guards independently.

Cautions

Cautions when using Guards: 1. Guards only handle Authentication, Authorization should be handled in business logic 2. Additional verification required for sensitive data even after passing Guards 3. Return 403 Forbidden on Guard failure (not 401 Unauthorized) 4. Consider order when using multiple Guards (general to specific) 5. Avoid complex business logic in Guards (simple permission checks only) 6. Resource ownership verification should be handled inside API methods

Next Steps

Authentication Setup

Building authentication system

Session Management

Session and token management

Context

Learn more about Context

@api Decorator

API decorator options