This video provides a comprehensive, step-by-step guide on implementing email verification in Laravel applications. It covers setting up Mailtrap for SMTP credentials, configuring the Laravel project, installing and using the Laravel Breeze package for authentication scaffolding, enabling email verification by modifying the User model and routes, and customizing verification emails. Additionally, it demonstrates three methods for manually verifying users without sending emails.
.env file with Mailtrap credentials and configure the database connection. Run php artisan migrate to create necessary database tables.composer require laravel/breeze) to scaffold authentication, registration, and password reset pages. Run php artisan breeze:install, npm install, and npm run dev to set up the frontend.User model, implement the MustVerifyEmail contract and re-run php artisan migrate. Define verification routes in web.php: email/verify, email/verify/{id}/{hash}, and email/verification-notification. Ensure these routes have specific names: verification.notice, verification.verify, and verification.send..env file (APP_NAME). Other elements like the subject line, body text, and button text can be customized within the AuthServiceProvider by configuring the toMailUsing method.markEmailAsVerified() method on the user.forceCreate() method and manually setting the email_verified_at field.fillable property in the User model to include the email_verified_at field and then using the create() method.The video outlines the following step-by-step process to set up email verification in Laravel:
Mailtrap Setup:
Laravel Project Configuration:
.env file and fill in the Mailtrap credentials (SMTP host, port, username, password).php artisan migrate to create necessary database tables.Install Laravel Breeze:
composer require laravel/breeze
php artisan breeze:install
npm install
npm run dev
Enable Email Verification Feature:
Open the app/Models/User.php file and add the MustVerifyEmail contract:
use Illuminate\Contracts\Auth\MustVerifyEmail;
class User extends Authenticatable implements MustVerifyEmail
{
// ... other class contents
}
Re-run the migration:
php artisan migrate
(This adds the email_verified_at field to the users table).
Add Verification Routes:
Open routes/web.php and add the following routes:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\VerificationController; // Assuming you have a VerificationController or use the framework's defaults
// ... other routes
Route::get('/email/verify', [VerificationController::class, 'show'])->middleware('auth')->name('verification.notice');
Route::get('/email/verify/{id}/{hash}', [VerificationController::class, 'verify'])->middleware(['auth', 'signed'])->name('verification.verify');
Route::post('/email/verification-notification', [VerificationController::class, 'resend'])->middleware(['auth', 'throttle:6,1'])->name('verification.send');
Note: The video shows the route definitions with specific names (verification.notice, verification.verify, verification.send) which are crucial for the verification process.
Testing Email Verification:
email_verified_at field in the database will be populated, and the user will be logged into the dashboard.Customizing Verification Emails:
Sender Name: Change APP_NAME in the .env file.
Subject, Body, Button Text: Open app/Providers/AuthServiceProvider.php, and within the boot method, configure the toMailUsing method:
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Carbon;
// ... inside the boot method of AuthServiceProvider
VerifyEmail::toMailUsing(function ($notifiable) {
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(60),
['id' => $notifiable->getKey(), 'hash' => sha1($notifiable->getEmailForVerification())]
);
return (new MailMessage)
->subject('Verify Your Email Address') // Customize subject
->line('Please click the link below to verify your email address.') // Customize body line 1
->action('Verify Email Address', $verificationUrl); // Customize button text and URL
});
Note: This code snippet demonstrates customizing the email itself. The video mentions modifying the AuthServiceProvider to achieve this.
Manual User Verification (Without Sending Emails):
Method 1: markEmailAsVerified()
Create a user and then call this method:
use App\Models\User;
$user = User::create([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => Hash::make('password'),
]);
$user->markEmailAsVerified();
Method 2: forceCreate() with email_verified_at
When creating the user, explicitly set the email_verified_at field:
use App\Models\User;
use Illuminate\Support\Carbon;
User::forceCreate([
'name' => 'Jane Doe',
'email' => 'jane.doe@example.com',
'password' => Hash::make('password'),
'email_verified_at' => Carbon::now(), // Set the verification timestamp
]);
Method 3: Modify fillable property in User Model
In app/Models/User.php, add email_verified_at to the $fillable array:
protected $fillable = [
'name',
'email',
'password',
'email_verified_at', // Add this
];
Then use the create method and explicitly set the field:
use App\Models\User;
use Illuminate\Support\Carbon;
User::create([
'name' => 'Peter Doe',
'email' => 'peter.doe@example.com',
'password' => Hash::make('password'),
'email_verified_at' => Carbon::now(), // Set the verification timestamp
]);