myshell/laravel-account-verification
Installation
composer require myshell/laravel-account-verification
Publish the package config and migrations:
php artisan vendor:publish --provider="MyShell\AccountVerification\AccountVerificationServiceProvider" --tag="config"
php artisan vendor:publish --provider="MyShell\AccountVerification\AccountVerificationServiceProvider" --tag="migrations"
Run migrations:
php artisan migrate
Configuration
Edit config/account-verification.php to set:
verification_email_template (default: emails.account_verification)verification_url (e.g., https://your-app.com/verify-email)expiration_minutes (default: 60)First Use Case Trigger verification for a user in a registration controller:
use MyShell\AccountVerification\Facades\AccountVerification;
// After user creation
AccountVerification::sendVerificationEmail($user);
Registration Flow
// In RegisterController
public function register(Request $request) {
$user = User::create($request->validated());
AccountVerification::sendVerificationEmail($user);
return redirect()->route('verification.notice');
}
Manual Verification
// In UserController
public function verifyEmail(User $user) {
if (AccountVerification::verify($user)) {
return back()->with('success', 'Email verified!');
}
return back()->with('error', 'Verification failed.');
}
Resend Verification
// In VerificationController
public function resend(Request $request) {
$request->user()->markEmailUnverified();
AccountVerification::sendVerificationEmail($request->user());
return back()->with('status', 'Verification link resent!');
}
php artisan vendor:publish --provider="MyShell\AccountVerification\AccountVerificationServiceProvider" --tag="views"
queue option in account-verification.php to true and use:
AccountVerification::queueVerificationEmail($user);
return response()->json([
'message' => 'Verification email sent',
'verification_url' => AccountVerification::generateVerificationUrl($user)
]);
Token Expiration
expiration_minutes (default: 60). Ensure users verify within this window or resend the email.account_verification_tokens table for expired tokens.Duplicate Tokens
clearOldTokens method in your service provider.Email Delivery Issues
from address in config/mail.php is valid. Test with a local mail server (e.g., Mailtrap) during development.$token = AccountVerification::generateToken($user);
logger()->debug("Generated token for {$user->email}: {$token}");
$isValid = AccountVerification::isTokenValid($user, $token);
logger()->debug("Token validity: {$isValid}");
Custom Verification Logic
Override the verify method in your service provider:
public function verify(User $user, $token = null)
{
if ($this->customCondition($user)) {
return false;
}
return parent::verify($user, $token);
}
Event Listeners Listen for verification events:
// In EventServiceProvider
protected $listen = [
'MyShell\AccountVerification\Events\VerificationAttempted' => [
'App\Listeners\LogVerificationAttempt',
],
];
Middleware for Protected Routes Use the provided middleware to protect routes:
Route::middleware(['verified'])->group(function () {
// Protected routes
});
verification_url in config must be absolute (e.g., https://example.com/verify). Relative URLs may break in production.mailable_class in account-verification.php matches your queue setup (e.g., MyShell\AccountVerification\Mail\VerifyEmail).How can I help you explore Laravel packages today?