zydnrbrn / authkit-php
This package provides a convenient way to integrate with AuthKit OAuth 2.0 authentication system in your Laravel applications.
Fund package maintenance!
zydnrbrn
Requires
- php: ^8.2|^8.3|^8.4
- guzzlehttp/guzzle: ^7.0
- illuminate/auth: ^9.0|^10.0|^11.0|^12.0
- illuminate/contracts: ^9.0|^10.0|^11.0|^12.0
- illuminate/http: ^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^7.0|^8.0|^9.0|^10.0
- pestphp/pest: ^3.0
- spatie/ray: ^1.28
README
This package provides a convenient way to integrate with AuthKit OAuth 2.0 authentication system in your Laravel applications. AuthKit allows you to easily implement secure authentication in your applications with support for OAuth 2.0 flows and API authentication.
Installation
You can install the package via composer:
composer require zydnrbrn/authkit-php
After installing, publish the configuration file:
php artisan vendor:publish --provider="Zydnrbrn\AuthKit\Providers\AuthKitServiceProvider"
Configuration
Add the following variables to your .env
file:
AUTHKIT_CLIENT_ID=your-client-id
AUTHKIT_CLIENT_SECRET=your-client-secret
AUTHKIT_REDIRECT_URI=https://your-app.com/callback
AUTHKIT_API_URL=https://api.authkit.com
Usage
OAuth Authorization Flow
use Zydnrbrn\AuthKit\Facades\AuthKit; class AuthController extends Controller { public function redirect() { return redirect()->to(AuthKit::getAuthorizationUrl()); } public function callback(Request $request) { // Exchange authorization code for token $token = AuthKit::exchangeCodeForToken($request->code); // Get the user $user = AuthKit::getUserFromToken($token); // Log the user in Auth::guard('authkit')->login($user); return redirect('/dashboard'); } }
Using the AuthKit Guard
In your config/auth.php
file:
'guards' => [ // ... 'authkit' => [ 'driver' => 'authkit', 'provider' => 'authkit_users', ], ], 'providers' => [ // ... 'authkit_users' => [ 'driver' => 'authkit', ], ],
Now you can use the guard in your application:
// Check if the user is authenticated if (Auth::guard('authkit')->check()) { // User is logged in } // Get the authenticated user $user = Auth::guard('authkit')->user();
Token Management
// Refresh an expired token $newToken = AuthKit::refreshToken($oldToken->getRefreshToken()); // Revoke a token AuthKit::revokeToken($token->getAccessToken()); // Validate a token $isValid = AuthKit::validateToken($token->getAccessToken());
Events
This package provides several events you can listen for:
Zydnrbrn\AuthKit\Events\TokenAcquired
: Fired when a new token is acquiredZydnrbrn\AuthKit\Events\TokenRefreshed
: Fired when a token is refreshedZydnrbrn\AuthKit\Events\AuthenticationFailed
: Fired when authentication fails
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- zydnrbrn
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Similar code found with 1 license type