tech-djoin / laravel-admin-ext-google-authenticator
Google Authenticator extension for laravel-admin
Requires
- php: ^7.4|^8.0
- encore/laravel-admin: ^1.8
- laravel/framework: ^8.75
- paragonie/constant_time_encoding: ^1.0|^2.0
- pragmarx/google2fa-laravel: ^2.0
- simplesoftwareio/simple-qrcode: ^4.2
Requires (Dev)
- orchestra/testbench: ^6.27
- pestphp/pest: ^1.23
- pestphp/pest-plugin-laravel: ^1.4
README
Two-Factor Authentication (2FA) extension for Laravel Admin using Google Authenticator.
Features
- Enable/Disable 2FA for individual admin users
- QR code scanning for easy setup
- Verification page for 2FA codes
- Configurable settings via .env file
- Middleware protection for admin routes
Installation
-
Install the package via Composer:
composer require tech-djoin/laravel-admin-ext-google-authenticator
-
Publish assets and configuration:
php artisan vendor:publish --provider="TechDjoin\LaravelAdminGoogleAuthenticator\GoogleAuthenticatorServiceProvider" --tag="config"
-
Customize the configuration file in
config/google2fa.php
as needed. -
Run migrations:
php artisan migrate
-
Override the
Administrator.php
file in the/vendor/encore/laravel-admin/src/Auth/Database
directory and update your admin user model (usually inApp/Models/AdminUser.php
):<?php namespace App\Models; use Encore\Admin\Auth\Database\Administrator; use Illuminate\Notifications\Notifiable; use TechDjoin\LaravelAdminGoogleAuthenticator\Traits\Google2FAAuthenticatableTrait; class AdminUser extends Administrator { use Notifiable; use Google2FAAuthenticatableTrait; protected $fillable = ['username', 'password', 'name', 'avatar', 'google2fa_secret']; public function setGoogle2faSecretAttribute($value) { $this->attributes['google2fa_secret'] = !is_null($value) ? encrypt($value) : null; } public function getGoogle2faSecretAttribute($value) { return isset($value) ? decrypt($value) : null; } }
-
Update
config/admin.php
:'auth' => [ 'model' => App\Models\AdminUser::class, ], 'database' => [ 'users_model' => App\Models\AdminUser::class ],
-
Install PHP Imagick extension for QR code generation.
Configuration (Optional)
You can configure the extension by editing config/google2fa.php
or setting values in your .env
file:
ADMIN_2FA_ENABLED=true
ADMIN_2FA_LIFETIME=0
ADMIN_2FA_KEEP_ALIVE=true
Installing Imagick PHP Extension
Windows
- Download and extract files from PECL Imagick Extension.
- Copy
php_imagick.dll
to the PHPext
folder. - Copy all
.dll
files from thebin
folder to the main PHP folder. - Edit
php.ini
, add the lineextension=imagick
. - Restart the web server.
Linux (Ubuntu/Debian)
-
Update repository:
sudo apt update
-
Install Imagick:
- PHP 7.x:
sudo apt install php-imagick
- PHP 8.x:
sudo apt install php8.x-imagick
- PHP 7.x:
-
Restart web server:
- Apache:
sudo systemctl restart apache2
- Nginx:
sudo systemctl restart nginx
- Apache:
Verify the installation by running php -m | grep imagick
.