acgranter/acgranter-symfony-authenticator
Installation Add the package via Composer:
composer require acgranter/acgranter-symfony-authenticator
Publish the configuration (if needed):
php artisan vendor:publish --provider="Acgranter\SymfonyAuthenticator\SymfonyAuthenticatorServiceProvider"
Basic Configuration
Register the authenticator in config/auth.php under the guards section:
'web' => [
'driver' => 'session',
'provider' => 'users',
'authenticator' => \Acgranter\SymfonyAuthenticator\Authenticator::class,
],
First Use Case: Login Flow
Use the Symfony AuthenticatorInterface to handle login logic in a controller or middleware:
use Acgranter\SymfonyAuthenticator\Authenticator;
public function login(Request $request, Authenticator $authenticator)
{
$credentials = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);
$user = User::where('email', $credentials['email'])->first();
if (!$authenticator->authenticate($user, $credentials['password'])) {
return back()->withErrors(['email' => 'Invalid credentials']);
}
// Redirect or proceed
}
Dependency Injection
Inject Authenticator into controllers, middleware, or services:
public function __construct(private Authenticator $authenticator) {}
Custom Auth Logic Extend the authenticator for custom rules:
use Acgranter\SymfonyAuthenticator\Authenticator as BaseAuthenticator;
class CustomAuthenticator extends BaseAuthenticator
{
public function authenticate(User $user, string $password): bool
{
if (!$user->isActive()) {
return false;
}
return parent::authenticate($user, $password);
}
}
Integration with Laravel’s Auth Use the package alongside Laravel’s built-in auth:
if (Auth::attempt($credentials)) {
$authenticator->authenticate(Auth::user(), $credentials['password']);
// Proceed
}
Middleware for Protected Routes Create middleware to enforce authentication:
public function handle(Request $request, Closure $next)
{
if (!$request->user() || !$this->authenticator->isAuthenticated($request->user())) {
abort(403);
}
return $next($request);
}
Session Dependency
The package relies on Symfony’s SessionInterface. Ensure your Laravel app has session configured (default is enabled).
SESSION_DRIVER in .env (e.g., SESSION_DRIVER=file).Password Hashing Mismatch
The package assumes Symfony’s password hashing. If using Laravel’s Hash::make(), ensure consistency:
Symfony\Component\Security\Core\Encoder\EncoderFactory or bridge Laravel’s hashing.User Provider Conflicts
The authenticator expects a Symfony UserProviderInterface. Laravel’s Illuminate\Contracts\Auth\Authenticatable may need adaptation:
Symfony\Component\Security\Core\User\UserInterface in your User model.APP_DEBUG=true in .env to log authenticator errors.authenticator.login.success or authenticator.login.failure events (if extended).Custom User Check
Override Authenticator::supports() to filter supported user classes:
public function supports(UserInterface $user): bool
{
return $user instanceof CustomUser;
}
Token-Based Auth
Extend for API tokens by implementing Symfony\Component\Security\Core\Authentication\Token\TokenInterface.
Rate Limiting
Combine with Laravel’s throttle middleware to limit login attempts:
Route::middleware(['throttle:5,1'])->group(...);
web guard. Specify a custom guard in config/auth.php:
'api' => [
'driver' => 'session',
'provider' => 'users',
'authenticator' => \Acgranter\SymfonyAuthenticator\Authenticator::class,
],
How can I help you explore Laravel packages today?