shahrakii / auty
Admin Authentication for Laravel
Requires
- php: ^8.1
- laravel/framework: ^10.0|^11.0|^12.0
This package is auto-updated.
Last update: 2026-03-25 06:23:43 UTC
README
Auty is a production-ready, fully-featured admin authentication and authorization package for Laravel 10+. It ships with a completely separate guard, role/permission system, OTP, 2FA, impersonation, session management, activity logging, and a clean built-in UI โ all in one package.
โจ Features at a Glance
| Feature | Details |
|---|---|
| Separate Admin Guard | Completely isolated from the default user guard |
| Role System | super_admin & admin roles with permission-based access control |
| OTP Auth | Email / SMS one-time codes with pluggable providers |
| 2FA (TOTP) | Google Authenticator compatible via pragmarx/google2fa |
| Impersonation | Super admins can view-as any admin with full audit trail |
| Session Management | Per-admin session tracking, revocation, suspicious login detection |
| Activity Logs | Every action logged with IP, user agent, method, URL |
| Brute-Force Protection | Rate limiting + account lock after failed attempts |
| Admin Panel UI | Dashboard, admin CRUD, role/permission editor, logs viewer |
| API Token Auth | Laravel Sanctum-powered API token support |
| Multi-Tenancy | Optional tenant_id scoping |
| Localization | All strings translatable via lang files |
| Events & Listeners | Extensible via standard Laravel events |
| Artisan Commands | auty:install, auty:create-admin, auty:assign-role |
๐ Installation
1. Require via Composer
composer require auty/auty
2. Run the installer
php artisan auty:install
This will:
- Publish config โ
config/auty.php - Publish migrations, views, lang files
- Run migrations
- Seed default roles & permissions
- Create your first Super Admin interactively
โ๏ธ Configuration
After installation, customize config/auty.php:
// config/auty.php 'prefix' => 'admin', // URL prefix: /admin/... 'guard' => 'admin', // auth guard name 'throttle' => [ 'enabled' => true, 'max_attempts' => 5, 'lock_account' => true, 'lock_duration_minutes' => 30, ], 'two_factor' => [ 'enabled' => true, 'enforce' => false, // require ALL admins to use 2FA ], 'otp' => [ 'enabled' => true, 'channel' => 'email', // email | sms 'provider' => \Auty\Services\Otp\EmailOtpProvider::class, ], 'sessions' => [ 'track' => true, 'max_per_admin' => 5, 'suspicious_login' => true, ],
๐ Package Structure
auty/
โโโ src/
โ โโโ AutyServiceProvider.php # Main service provider
โ โโโ Console/Commands/
โ โ โโโ InstallCommand.php # php artisan auty:install
โ โ โโโ CreateAdminCommand.php # php artisan auty:create-admin
โ โ โโโ AssignRoleCommand.php # php artisan auty:assign-role
โ โโโ Http/
โ โ โโโ Controllers/
โ โ โ โโโ Auth/
โ โ โ โ โโโ LoginController.php
โ โ โ โ โโโ LogoutController.php
โ โ โ โ โโโ ForgotPasswordController.php
โ โ โ โ โโโ ResetPasswordController.php
โ โ โ โ โโโ OtpController.php
โ โ โ โ โโโ TwoFactorController.php
โ โ โ โโโ DashboardController.php
โ โ โ โโโ AdminController.php
โ โ โ โโโ ProfileController.php
โ โ โ โโโ RoleController.php
โ โ โ โโโ ActivityLogController.php
โ โ โ โโโ SessionController.php
โ โ โ โโโ ImpersonationController.php
โ โ โโโ Middleware/
โ โ โโโ AdminAuthenticate.php # auty.auth
โ โ โโโ AdminRole.php # auty.role:super_admin,admin
โ โ โโโ AdminPermission.php # auty.permission:admins.view
โ โ โโโ SuperAdmin.php # auty.super
โ โ โโโ OtpVerified.php # auty.otp
โ โ โโโ TwoFactorVerified.php # auty.2fa
โ โโโ Models/
โ โ โโโ Admin.php
โ โ โโโ AdminRole.php
โ โ โโโ AdminPermission.php
โ โ โโโ AdminActivityLog.php
โ โ โโโ AdminSession.php
โ โ โโโ AdminOtp.php
โ โโโ Services/
โ โ โโโ OtpService.php
โ โ โโโ TwoFactorService.php
โ โ โโโ ImpersonationService.php
โ โ โโโ SessionService.php
โ โ โโโ ActivityLogService.php
โ โ โโโ Otp/EmailOtpProvider.php
โ โโโ Traits/
โ โ โโโ HasRoles.php
โ โ โโโ HasPermissions.php
โ โ โโโ HasTwoFactor.php
โ โ โโโ HasOtp.php
โ โ โโโ LogsActivity.php
โ โโโ Events/
โ โ โโโ AdminLoggedIn.php
โ โ โโโ AdminLoggedOut.php
โ โ โโโ AdminFailedLogin.php
โ โ โโโ OtpRequested.php
โ โ โโโ ImpersonationStarted.php
โ โ โโโ ImpersonationEnded.php
โ โโโ Listeners/
โ โ โโโ LogAdminLogin.php
โ โ โโโ LogAdminLogout.php
โ โ โโโ LogFailedLogin.php
โ โ โโโ LogImpersonation.php
โ โ โโโ SendOtpNotification.php
โ โโโ Policies/
โ โ โโโ AdminPolicy.php
โ โโโ Contracts/
โ โโโ OtpProvider.php
โโโ config/auty.php
โโโ database/migrations/
โ โโโ ..._create_admins_table.php
โ โโโ ..._create_admin_roles_table.php
โ โโโ ..._create_admin_activity_logs_table.php
โ โโโ ..._create_admin_sessions_table.php
โ โโโ ..._create_admin_otps_table.php
โโโ resources/
โ โโโ views/
โ โ โโโ layouts/{app,auth}.blade.php
โ โ โโโ auth/{login,otp,two-factor,forgot-password,reset-password}.blade.php
โ โ โโโ dashboard/index.blade.php
โ โ โโโ admins/{index,create,edit}.blade.php
โ โ โโโ roles/{index,create,edit}.blade.php
โ โ โโโ logs/index.blade.php
โ โ โโโ sessions/index.blade.php
โ โ โโโ profile/index.blade.php
โ โโโ lang/en/{auth,admin,role,profile,session,impersonation}.php
โโโ routes/{web.php,api.php}
๐ก๏ธ Guard Configuration
The package automatically configures a separate admin guard. You can inspect/override in config/auth.php:
'guards' => [ 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], ], 'providers' => [ 'admins' => [ 'driver' => 'eloquent', 'model' => \Auty\Models\Admin::class, ], ], 'passwords' => [ 'admins' => [ 'provider' => 'admins', 'table' => 'admin_password_reset_tokens', 'expire' => 60, ], ],
๐ Middleware Usage
All middleware are registered automatically:
// Protect a route โ admin must be authenticated Route::middleware('auty.auth')->group(...); // Role-based access Route::middleware('auty.role:super_admin')->group(...); Route::middleware('auty.role:admin,super_admin')->group(...); // Permission-based access Route::middleware('auty.permission:admins.view')->group(...); Route::middleware('auty.permission:admins.edit,admins.create')->group(...); // Super admin only Route::middleware('auty.super')->group(...); // Require OTP verification Route::middleware('auty.otp')->group(...); // Require 2FA verification Route::middleware('auty.2fa')->group(...);
๐ฅ Roles & Permissions
Assigning roles
// Via code $admin->assignRole('admin'); $admin->assignRole('super_admin', 'admin'); // multiple $admin->syncRoles(['admin']); $admin->removeRole('admin'); // Via Artisan php artisan auty:assign-role admin@example.com super_admin
Checking roles
$admin->hasRole('super_admin'); $admin->hasAnyRole(['admin', 'editor']); $admin->hasAllRoles(['admin', 'editor']); $admin->isSuperAdmin(); // shortcut
Permissions
// Give direct permission $admin->givePermission('admins.create'); // Give to role $role->givePermission('admins.view'); // Check $admin->hasPermission('admins.delete'); $admin->hasAnyPermission(['admins.edit', 'admins.create']); // Gate integration Gate::allows('admins.view'); $admin->can('admins.view');
๐ OTP Authentication Flow
1. Admin submits email/password โ login succeeds
2. If config('auty.otp.enabled') is true:
โ OTP is generated and fired via OtpRequested event
โ SendOtpNotification listener delivers OTP to email/SMS
โ Admin is redirected to /admin/otp
3. Admin enters code โ verified via OtpService::verify()
4. Session key `auty_otp_verified` is set
5. Subsequent requests pass through OtpVerified middleware
Custom OTP Provider
// app/Otp/SmsOtpProvider.php use Auty\Contracts\OtpProvider; class SmsOtpProvider implements OtpProvider { public function send(Admin $admin, AdminOtp $otp): void { // Send SMS via Twilio, Vonage, etc. app(TwilioClient::class)->messages->create($admin->phone, [ 'from' => config('services.twilio.from'), 'body' => "Your login code: {$otp->code}", ]); } } // config/auty.php 'otp' => [ 'provider' => \App\Otp\SmsOtpProvider::class, 'channel' => 'sms', ],
๐ต๏ธ Impersonation
Super admins can view the panel as any other admin:
// Start impersonating $impersonation = app(\Auty\Services\ImpersonationService::class); $impersonation->impersonate($superAdmin, $targetAdmin); // Stop $impersonation->stopImpersonating(); // Check $impersonation->isImpersonating(); // bool $impersonation->getOriginalAdmin(); // Admin|null
UI: Click "View As" on the admins list. A yellow banner appears at the top of every page while impersonating. Full activity log is recorded.
๐ Database Schema
-- admins id, name, email, password, phone, avatar, is_active, is_locked, locked_until, failed_login_count, last_login_at, last_login_ip, two_factor_secret, two_factor_enabled, email_verified_at, tenant_id (nullable), remember_token, deleted_at, timestamps -- admin_roles id, name, label, description, tenant_id, timestamps -- admin_permissions id, name, label, group, description, timestamps -- admin_role_permission (pivot) role_id, permission_id -- admin_role_assignments (pivot) admin_id, role_id, timestamps -- admin_direct_permissions (pivot) admin_id, permission_id, timestamps -- admin_activity_logs id, admin_id, impersonated_by, event, description, properties (json), ip_address, user_agent, url, method, created_at -- admin_sessions id, admin_id, session_id, ip_address, user_agent, device_type, device_name, browser, platform, location, last_activity, is_current, payload (json), timestamps -- admin_otps id, admin_id, code, channel, used, attempts, expires_at, timestamps -- admin_password_reset_tokens email, token, created_at
๐ก Events
Listen to Auty events in your EventServiceProvider or any event listener:
use Auty\Events\AdminLoggedIn; use Auty\Events\AdminLoggedOut; use Auty\Events\AdminFailedLogin; use Auty\Events\OtpRequested; use Auty\Events\ImpersonationStarted; use Auty\Events\ImpersonationEnded; // Example listener Event::listen(AdminLoggedIn::class, function (AdminLoggedIn $event) { logger("Admin {$event->admin->email} logged in from {$event->ip}"); });
๐ Localization
Publish and edit the lang files:
php artisan vendor:publish --tag=auty-lang
Files appear in lang/vendor/auty/{locale}/. Supports any locale via:
// config/auty.php 'locale' => 'ar', // Arabic, French, etc.
๐ Security Checklist
Auty ships with these protections enabled by default:
- Separate authentication guard (no user/admin collision)
- Rate limiting per email+IP combination
- Account lock after N failed attempts (configurable)
- Soft deletes on Admin model
- Password hashed via
Hash::make()with rehash detection - CSRF protection on all forms
- Session regeneration after login
- Suspicious login detection (IP change)
- 2FA with TOTP (RFC 6238)
- OTP with expiry & attempt limiting (max 3 attempts per OTP)
- Impersonation restricted to
super_adminrole - Activity logging with impersonator tracking
- IP whitelist/blacklist support
- Session invalidation on logout
๐งช Running Tests
cd auty
composer install
vendor/bin/phpunit
๐ค Extending
Custom Admin Model
// config/auty.php 'models' => [ 'admin' => \App\Models\MyAdmin::class, ], // App\Models\MyAdmin class MyAdmin extends \Auty\Models\Admin { protected $fillable = [ ...parent::getFillable(), 'department', ]; }
Custom OTP Provider (SMS via Vonage)
class VonageOtpProvider implements \Auty\Contracts\OtpProvider { public function send(Admin $admin, AdminOtp $otp): void { // Vonage SMS logic } }
๐ License
MIT ยฉ Auty Package