Tyro Documentation
Complete guide to Authentication, Authorization, and Role & Privilege Management for Laravel 12 and 13.
Introduction
Tyro is the ultimate Authentication, Authorization, and Role & Privilege Management solution for Laravel. Think of it as a Swiss Army knife that handles everything from user authentication and fine-grained access control to user suspension workflows.
With native Sanctum integration, 44 powerful CLI commands, specialized Blade directives, ready-made middleware, and optional REST API endpoints, Tyro saves you weeks of development time while providing a robust, enterprise-grade architecture.
Requirements
Before installing Tyro, ensure your environment meets these requirements:
- PHP: 8.2 or higher
- Laravel: 12.0 or 13.0
- Laravel Sanctum: Required (auto-installed)
- Database: MySQL, PostgreSQL, SQLite, or SQL Server
Installation
Step 1: Install the Package
Require the package globally via Composer:
composer require hasinhayder/tyro
Step 2: Run the Installer
Run the powerful, all-in-one setup command:
php artisan tyro:install
This command automatically:
- Calls Laravel's
install:apito configure Sanctum - Runs all required database migrations
- Seeds default system roles and privileges
- Wires up your
Usermodel with the required Tyro traits
--force flag in CI/CD pipelines to bypass prompts.
Updating Tyro
When updating Tyro using composer, you must run migrations to apply any potential database schema changes.
# Update the package
composer update hasinhayder/tyro
# Run migrations to apply new schema changes
php artisan migrate
Quick Start
After installation, you have a fully functional authentication system. Let's see it in action:
1. Login to the API
curl -X POST http://localhost/api/login \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{"email":"admin@tyro.project","password":"tyro"}'
2. Protect Your Routes
Immediately start protecting routes using the injected middleware aliases:
Route::middleware(['auth', 'role:admin'])
->get('/admin/dashboard', DashboardController::class);
3. Check Permissions in Code
Utilize Tyro's fluent API directly on the User model:
// Check roles explicitly
if ($user->hasRole('editor')) { ... }
// Check granular privileges
if ($user->can('reports.run')) { ... }
Roles & Privileges Architecture
Tyro operates on a pure RBAC (Role-Based Access Control) architecture. Users are assigned Roles (e.g., "admin", "editor"). Roles contain collections of Privileges (e.g., "users.delete", "reports.view"). Users automatically inherit all privileges assigned to their roles.
Default System Roles
Upon installation, Tyro seeds the database with foundational roles:
| Role Name | Slug | Description |
|---|---|---|
| Super Admin | super-admin |
Absolute system access. Cannot be deleted via CLI. |
| Administrator | admin |
Administrative access to manage users, roles, and content. |
| Editor | editor |
Standard content management privileges. |
| User | user |
The default role assigned to new registrations. |
admin and super-admin) are protected at the database level and cannot be accidentally deleted via standard API/CLI methods.
Middleware Aliases
Tyro registers 8 powerful middleware aliases for enforcing access control directly at the routing layer.
| Middleware Alias | Example Syntax | Behavior |
|---|---|---|
role |
role:admin |
User must have the exact role. |
roles |
roles:admin,editor |
User must have any one of the listed roles. |
privilege |
privilege:users.delete |
User must possess the exact privilege. |
privileges |
privileges:x,y |
User must possess any one of the listed privileges. |
ability |
ability:admin,x |
User must possess ALL listed abilities (roles or privileges). |
abilities |
abilities:admin,y |
User must possess ANY of the listed abilities. |
tyro.log |
tyro.log |
Silently audits and logs the request/response cycle for the route. |
Blade Directives
Keep your views clean. Tyro injects 7 native Blade directives to handle UI rendering conditionally based on the authenticated user's access level.
@hasRole('admin')
<div>Rendered only for admins</div>
@endhasRole
@hasAnyRole('admin', 'editor')
<div>Rendered for admins OR editors</div>
@endhasAnyRole
@hasPrivilege('reports.export')
<button>Export CSV</button>
@endhasPrivilege
{{-- The @userCan directive wraps Laravel's native can() --}}
@userCan('users.delete')
<button class="text-red-500">Delete</button>
@enduserCan
User Suspension
Never hard-delete a malicious user again. Tyro includes a first-class suspension workflow. When a user is suspended, all of their active Sanctum tokens are instantly revoked, and they are prevented from logging in.
# Suspend a user with an audit reason
php artisan tyro:user-suspend --user=malicious@email.com --reason="Spam activity"
# View all suspended users
php artisan tyro:user-suspended
# Restore access
php artisan tyro:user-unsuspend --user=malicious@email.com
// Suspend
$user->suspend('Violation of TOS');
// Check status
if ($user->isSuspended()) {
echo $user->getSuspensionReason();
}
Command Reference Guide
Tyro bundles 44 Artisan commands. Here are the most critical ones for daily management.
User & Role Commands
Token & Auth Commands
REST API Endpoints
Tyro optionally provides a full suite of production-ready API endpoints.
If you don't need them, set TYRO_DISABLE_API=true in your .env.
Authentication
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/login |
Authenticate and receive a Sanctum token |
| POST | /api/users |
Public registration endpoint |
| GET | /api/me |
Get the authenticated user's profile and roles |
Admin Endpoints (Requires admin role)
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/users |
List all registered users |
| POST | /api/users/{id}/suspend |
Suspend a user account |
| POST | /api/roles |
Create a new system role |
| POST | /api/users/{id}/roles |
Assign a role to a user |
Immutable Audit Trail
Tyro automatically records an immutable ledger of all critical administrative actions. It tracks Who did what, to whom, and when.
Monitored Events
- User suspension and restoration (includes reasons)
- Role assignment and removal
- Role/Privilege creation, modification, and deletion
Viewing Logs
# View the most recent 20 events
php artisan tyro:audit-list
# Filter by specific event type
php artisan tyro:audit-list --event=user.suspended
# Automatic log rotation (purge logs older than 30 days)
php artisan tyro:audit-purge
Need more help? Check the source code or open an issue.
Visit GitHub Repository