Installation
Add the bundle to your composer.json:
composer require beelab/user-password-bundle
Register the bundle in config/bundles.php:
BeeLab\UserPasswordBundle\BeeLabUserPasswordBundle::class => ['all' => true],
Configuration Publish the default config:
php artisan vendor:publish --tag=beelab_user_password_config
Update config/beelab_user_password.php with your email settings (e.g., mailer transport, from address).
First Use Case: Password Reset Flow
LostPasswordController):
use BeeLab\UserPasswordBundle\Controller\LostPasswordController;
@BeeLabUserPassword/reset_password.html.twig) for the reset form.Requesting a Password Reset
LostPasswordController or extend it to customize logic:
public function requestReset(Request $request)
{
$email = $request->request->get('email');
$this->get('beelab_user_password.password_resetter')->sendResetLink($email);
return $this->render('your_template.html.twig');
}
PasswordResetter service handles token generation, email dispatch, and user validation.Resetting a Password
ResetPasswordController validates tokens and updates passwords:
public function reset(Request $request, $token)
{
$this->get('beelab_user_password.password_resetter')->resetPassword($token, $request->request->get('password'));
return $this->redirectToRoute('home');
}
token_ttl (default: 1 hour) in config.Customizing Emails
php artisan vendor:publish --tag=beelab_user_password_translation
BeeLab\UserPasswordBundle\Event\PasswordResetEvent to modify email content dynamically.Integration with BeelabUserBundle
BeeLab\UserBundle is installed. Ensure your User entity extends BeeLab\UserBundle\Entity\User and includes:
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $resetToken;
resetToken and resetTokenExpiresAt.Token Management
PasswordResetter service:
$token = $this->get('beelab_user_password.password_resetter')->generateToken($user);
$isValid = $this->get('beelab_user_password.password_resetter')->isTokenValid($token, $user);
BeeLab\UserPasswordBundle\Service\TokenGenerator.Event-Driven Extensions
PasswordResetEvent to log/resend emails:
// src/EventListener/PasswordResetListener.php
public function onPasswordReset(PasswordResetEvent $event)
{
// Custom logic (e.g., analytics, notifications)
}
services.yaml:
services:
App\EventListener\PasswordResetListener:
tags:
- { name: kernel.event_listener, event: beelab_user_password.reset, method: onPasswordReset }
Testing
PasswordResetter in tests:
$resetter = $this->createMock(PasswordResetter::class);
$resetter->method('sendResetLink')->willReturn(true);
$this->container->set('beelab_user_password.password_resetter', $resetter);
BeeLab\UserPasswordBundle\Tests as a reference for test cases.Missing BeelabUserBundle
Class 'BeeLab\UserBundle\Entity\User' not found.beelab/user-bundle is installed and your User entity extends the correct base class.Token Expiry
token_ttl in config and ensure resetTokenExpiresAt is updated during token generation.# config/beelab_user_password.php
token_ttl: 86400 # 24 hours in seconds
Email Delivery Failures
mailer transport in config (e.g., smtp, sendmail).from address is valid and not blacklisted.CSRF Protection
{{ csrf_token('reset_password') }}
Database Schema Mismatch
Column 'resetToken' not found.// Migration file
$table->string('reset_token')->nullable();
$table->datetime('reset_token_expires_at')->nullable();
Custom Token Storage
# config/beelab_user_password.php
token_storage: redis
predis/predis and Redis server.Rate Limiting
// Extend PasswordResetter
public function sendResetLink($email)
{
if ($this->isRateLimited($email)) {
throw new \RuntimeException('Too many requests. Try again later.');
}
// ... existing logic
}
Localization
php artisan vendor:publish --tag=beelab_user_password_translation --force
resources/translations/beelab_user_password.xx.yaml.Logging
// In a listener
$this->logger->info('Password reset requested', [
'email' => $event->getEmail(),
'ip' => $request->getClientIp(),
]);
Testing Tokens
PasswordResetter directly in tests to avoid email delivery:
$resetter = $this->get('beelab_user_password.password_resetter');
$resetter->setMailerMock(true); // Disables email sending
How can I help you explore Laravel packages today?