stia / authid-php
There is no license information available for the latest version (1.0.2) of this package.
SDK / Plugin to handle Authenticator for Auth ID
1.0.2
2025-03-11 08:50 UTC
Requires
- php: ^8.1
- bacon/bacon-qr-code: ^3.0
- illuminate/support: ^10.0
- pragmarx/google2fa: ^8.0
README
- Laravel 10 or later
- PHP 8.1 or later
Instalation Guide
Install dependency with composer
composer require stia/authid-php
Publish the config file
php artisan vendor:publish --provider="Stia\AuthidPhp\AuthIDServiceProvider"
Run the migration
php artisan migrate
To enable 2 FA, add the following code to the User model
use TwoFactorAuthenticatable;
Example:
class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; use TwoFactorAuthenticatable; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ ...
Method Usage
Check is user already have recovery codes
$user = User::find($id); $isEnabled = $user->hasEnabledTwoFactorAuthentication(); return $isEnabled;
Generate new recovery codes
$user = User::find($id); $user->generateNewRecoveryCode();
Get user recovery codes
$user = User::find($id); $recoveryCodes = $user->recoveryCodes(); return $recoveryCodes;
Enable 2FA and generate QR code
namespace App\Http\Controllers; use Stia\AuthidPhp\Actions\EnableTwoFactorAuthentication; class UserController extends Controller { public function enableTwoFactorAuthentication(EnableTwoFactorAuthentication $enableAction) { $user = auth()->user(); $enableAction($user); return $user->twoFactorQrCodeSvg(); } }
Validate 2FA
namespace App\Http\Controllers; use Illuminate\Support\Facades\Request; use Stia\AuthidPhp\Actions\EnableTwoFactorAuthentication; use Stia\AuthidPhp\Actions\IsTwoFactorAuthenticationValid; class UserController extends Controller { public function validateTwoFactorAuthentication(Request $request, IsTwoFactorAuthenticationValid $confirm) { if ($confirm($request->user(), $request->code)) { return 'OK'; } return 'Code not valid'; } ...