PHP code example of serenity_technologies / admin-dashboard-guard

1. Go to this page and download the library: Download serenity_technologies/admin-dashboard-guard library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serenity_technologies / admin-dashboard-guard example snippets


// In the migration:
$table->string('passphrase')->nullable();

'guards' => [
    'admin' => [
        'driver'   => 'session',
        'provider' => 'admins',
    ],
],

'providers' => [
    'admins' => [
        'driver' => 'eloquent',
        'model'  => App\Models\Admin::class,
    ],
],

'middleware' => ['web', 'admin.horizon'],

protected function gate(): void
{
    Gate::define('viewTelescope', fn () => auth('admin')->check());
}

Route::middleware(['web', 'admin.telescope'])->group(function () {
    // Telescope routes
});

// Protect Pulse
Route::middleware(['web', 'admin.pulse'])->group(function () {
    Route::get('/pulse', ...);
});

// Protect a custom admin panel
Route::middleware(['web', 'admin.myadmin'])->prefix('admin')->group(function () {
    // your admin routes
});

'protected_pages' => [

    'horizon' => [
        'path'           => 'horizon',
        'gate'           => 'viewHorizon',
        'session_prefix' => 'horizon',
        'passphrase_ttl' => 10,
        'session_ttl'    => 60,
        'theme_color'    => 'purple',
    ],

    'telescope' => [
        'path'           => 'telescope',
        'gate'           => 'viewTelescope',
        'session_prefix' => 'telescope',
        'passphrase_ttl' => 10,
        'session_ttl'    => 60,
        'theme_color'    => 'indigo',
    ],

    // Add any new page here:
    'pulse' => [
        'path'           => 'pulse',
        'gate'           => 'viewPulse',   // set null to skip gate registration
        'session_prefix' => 'pulse',
        'passphrase_ttl' => 10,
        'session_ttl'    => 60,
        'theme_color'    => 'rose',
    ],

],

'stealth_mode' => true,

'protected_pages' => [
    'horizon' => [
        // ...
        'stealth_mode' => true,   // Horizon: 404 when unauthenticated
    ],
    'telescope' => [
        // ...
        'stealth_mode' => false,  // Telescope: still shows the passphrase form
    ],
],

// config/admin-dashboard-guard.php

return [

    // The Laravel auth guard used to authenticate the admin.
    'guard' => env('ADMIN_DASHBOARD_GUARD', 'admin'),

    // Fully-qualified Eloquent model class.
    'model' => env('ADMIN_DASHBOARD_MODEL', null),

    // Column names on the model.
    'passphrase_column' => env('ADMIN_DASHBOARD_PASSPHRASE_COLUMN', 'passphrase'),
    'password_column'   => env('ADMIN_DASHBOARD_PASSWORD_COLUMN', 'password'),

    // Accept a valid Sanctum personal-access token as an alternative to the
    // session flow. Requires laravel/sanctum.
    'sanctum_support' => env('ADMIN_DASHBOARD_SANCTUM', true),

    // Pages to protect — see "Adding a New Protected Page" above.
    'protected_pages' => [ /* ... */ ],

];

use SerenityTechnologies\AdminDashboardGuard\Mail\AdminPassphrase;

Mail::to($admin)->send(new AdminPassphrase($passphrase, 'Your Dashboard Passphrase'));

namespace App\Auth;

use Illuminate\Http\Request;

class AuthorizeAdminDashboardAccess
{
    public function __invoke(mixed $admin, Request $request, string $tool): bool
    {
        return $admin && in_array($admin->role, ['SuperAdmin', 'Admin', 'Developer']);
    }
}

'condition' => App\Auth\AuthorizeAdminDashboardAccess::class,

'protected_pages' => [
    'horizon' => [
        'path' => 'horizon',
        'condition' => App\Auth\AuthorizeAdminDashboardAccess::class,
    ],
],

use SerenityTechnologies\AdminDashboardGuard\Http\Middleware\AuthenticateAdminForDashboard;

AuthenticateAdminForDashboard::checkUsing(function ($admin, $request, $tool) {
    return in_array($admin->role, ['SuperAdmin', 'Admin', 'Developer']);
});
bash
php artisan vendor:publish --tag=admin-dashboard-guard-config
bash
php artisan migrate
bash
php artisan admin-guard:generate-passphrase [email protected]
bash
php artisan admin-guard:generate-passphrases
bash
php artisan admin-guard:remove-passphrase [email protected]
bash
php artisan admin-guard:clear-passphrases
bash
php artisan vendor:publish --tag=admin-dashboard-guard-views