baks-dev/auth-email
Laravel/PHP package for user authentication via email. Provides installable configuration/assets and Doctrine migration support to update the database schema. Includes PHPUnit tests (auth-email group) and requires PHP 8.4+.
Installation
Run composer require baks-dev/auth-email in your Laravel project.
Ensure your project meets the PHP 8.4+ requirement and has baks-dev/core and baks-dev/captcha installed (or update dependencies if missing).
Publish Assets & Migrations Execute:
php artisan vendor:publish --provider="BaksDev\AuthEmail\AuthEmailServiceProvider"
php artisan baks:assets:install
php artisan migrate
This sets up:
auth_email_verification_tokens).resources/views/auth-email/).config/auth-email.php).First Use Case: Email-Based Login
Add the auth guard to config/auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'email', // Use the email provider
],
],
Use the LoginController (published with assets) or extend it:
use BaksDev\AuthEmail\Http\Controllers\Auth\LoginController;
Email Verification Flow
$user->sendEmailVerificationNotification();
(Extend BaksDev\AuthEmail\Notifications\VerifyEmail if needed.)auth-email.php). Verify via:
Auth::guard('web')->verifyEmail($user);
Custom Login Logic
app/Http/Controllers/Auth/LoginController:
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
'captcha' => 'required|captcha', // Uses baks-dev/captcha
]);
return $this->authenticate($request); // Uses AuthEmail guard
}
Password Reset
use BaksDev\AuthEmail\Http\Controllers\Auth\ResetPasswordController;
php artisan vendor:publish --tag=auth-email-views
email provider:
Sanctum::guard('web'); // Uses the email guard
users provider:
'providers' => [
'email' => [
'driver' => 'eloquent',
'model' => \App\Models\User::class,
],
'users' => [
'driver' => 'eloquent',
'model' => \App\Models\LegacyUser::class,
],
],
baks-dev/captcha is configured in config/captcha.php for login/reset flows.Missing Dependencies
baks-dev/core or baks-dev/captcha are outdated, the package may fail silently. Run:
composer update baks-dev/core baks-dev/captcha
Token Expiry Issues
config/auth-email.php:
'verification' => [
'expire' => 60, // Minutes
],
auth_email_verification_tokens table for stale tokens.Locale/Translation Conflicts
php artisan vendor:publish --tag=auth-email-lang
resources/lang/{locale}/auth-email.php.CSRF Token Mismatch
VerifyCsrfToken middleware is excluded or configured for stateless APIs.EventServiceProvider:
protected $listen = [
\BaksDev\AuthEmail\Events\VerificationAttempted::class => [
\App\Listeners\LogVerificationAttempt::class,
],
];
VerifyEmail middleware is applied to protected routes:
Route::middleware(['auth', 'verified'])->group(function () { ... });
Custom User Model
Extend the default BaksDev\AuthEmail\Models\User or bind your model in AuthServiceProvider:
public function boot()
{
Auth::provider('email', function ($app) {
return new UserProvider($app['hash'], \App\Models\CustomUser::class);
});
}
Email Templates Override default Blade templates:
resources/views/auth-email/verify.blade.phpresources/views/auth-email/reset.blade.phpRate Limiting
Customize rate limits in app/Http/Middleware/ThrottleNodes:
protected function limits()
{
return [
'email-verification' => ['max' => 5, 'perMinute' => true],
];
}
Two-Factor Auth (2FA)
Integrate with Laravel’s two-factor package:
use BaksDev\AuthEmail\Traits\TwoFactorAuth;
class User extends Authenticatable {
use TwoFactorAuth;
}
How can I help you explore Laravel packages today?