dginxreal/laravel-auth
dginxreal/laravel-auth is a lightweight Laravel authentication package that helps you add common auth features to your app with minimal setup, providing a starting point for implementing login/session-based access control.
Installation
composer require dginxreal/laravel-auth
php artisan vendor:publish --provider="Dginxreal\Auth\AuthServiceProvider" --tag="migrations"
php artisan migrate
Configuration Publish the config file:
php artisan vendor:publish --provider="Dginxreal\Auth\AuthServiceProvider" --tag="config"
Review config/auth.php for customization (e.g., guard names, user model).
First Use Case
Register a route in routes/web.php:
use Dginxreal\Auth\Facades\Auth;
Route::get('/login', function () {
return Auth::attempt(['email' => 'user@example.com', 'password' => 'password']);
});
Authentication
Auth::attempt($credentials); // Login
Auth::logout(); // Logout
Auth::loginUsingId($userId); // Force login
Auth::logoutOtherDevices($password); // Multi-device logout
User Management
use Dginxreal\Auth\Facades\Auth;
$user = Auth::register(['name' => 'John', 'email' => 'john@example.com', 'password' => 'secret']);
Auth::sendPasswordResetLink(['email' => 'user@example.com']);
Auth::resetPassword($token, ['password' => 'newpassword']);
Middleware Integration Use the package’s built-in middleware:
Route::middleware(['auth:api'])->group(function () {
// Protected API routes
});
AuthServiceProvider to add custom guards:
protected function guards()
{
return [
'web' => ['driver' => 'session', 'provider' => 'users'],
'admin' => ['driver' => 'session', 'provider' => 'admins'],
];
}
Attempting, Authenticated):
Auth::listen(function ($event) {
Log::info('Auth event: ' . $event->fires);
});
Migration Conflicts
users table migrations. Run:
php artisan vendor:publish --tag="migrations" --force
Then manually resolve conflicts.Session Driver Mismatch
api guard, ensure SESSION_DRIVER in .env is set to database or redis for consistency.Password Hashing
Hash facade. If customizing, ensure the users table’s password column uses the same hashing algorithm.Auth::validate($credentials) for manual validation before attempt() to debug credential issues.php artisan config:clear
php artisan cache:clear
php artisan session:clear
Auth::listen() to trace auth events in real-time.Custom User Model
Override the default user model in config/auth.php:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\CustomUser::class,
],
],
Custom Auth Logic
Extend the AuthManager class by binding a custom implementation in the service provider:
$this->app->bind('auth', function ($app) {
return new \App\Services\CustomAuthManager($app);
});
Rate Limiting
Integrate with Laravel’s throttle middleware for login attempts:
Route::post('/login', function () {
return Auth::attempt(request()->only(['email', 'password']));
})->middleware('throttle:5,1');
How can I help you explore Laravel packages today?