Admin Authentication for Laravel

Maintainers

Package info

github.com/Shahrakii/Auty

pkg:composer/shahrakii/auty

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

dev-main 2026-02-25 06:04 UTC

This package is auto-updated.

Last update: 2026-03-25 06:23:43 UTC


README

Laravel PHP License

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_admin role
  • 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