Cover Image for Laravel 5.8 Email Verification Example
179 views

Laravel 5.8 Email Verification Example

The Laravel 5.8 has email verification can be implemented to ensure that users’ email addresses are valid and verified before they can access certain parts of your application. Here’s a step-by-step example of how to set up email verification in a Laravel 5.8 application:

  1. Create a New Laravel Project: If you haven’t already, create a new Laravel project using Composer:
Bash
 composer create-project --prefer-dist laravel/laravel laravel_email_verification
  1. Configure Environment Variables: Open the .env file in your project’s root directory and configure your mail settings. You should specify your SMTP settings to allow Laravel to send email verification links:
PHP
 MAIL_DRIVER=smtp
 MAIL_HOST=your-smtp-host
 MAIL_PORT=587
 MAIL_USERNAME=your-email@example.com
 MAIL_PASSWORD=your-email-password
 MAIL_ENCRYPTION=tls
  1. Generate Authentication Scaffolding: Laravel provides a command to generate authentication scaffolding. Run the following command to generate the necessary files for registration and login:
Bash
 php artisan make:auth
  1. Enable Email Verification: In Laravel 5.8, email verification is built into the authentication scaffolding. Open the app/Http/Controllers/Auth/RegisterController.php file and add the MustVerifyEmail interface to the RegisterController class:
PHP
 use Illuminate\Foundation\Auth\RegistersUsers;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Validator;
 use Illuminate\Auth\Events\Registered;
 use Illuminate\Contracts\Auth\MustVerifyEmail; // Add this line

 class RegisterController extends Controller implements MustVerifyEmail // Add this line
 {
     // ...
 }
  1. Configure the User Model: Open the app/User.php file and make sure your User model implements the MustVerifyEmail interface:
PHP
 use Illuminate\Contracts\Auth\MustVerifyEmail; // Add this line

 class User extends Authenticatable implements MustVerifyEmail // Add this line
 {
     // ...
 }
  1. Update the web Middleware Group: In the app/Http/Kernel.php file, add the verified middleware to the web middleware group. This middleware ensures that users are redirected to the email verification notice if they haven’t verified their email address:
PHP
 protected $middlewareGroups = [
     'web' => [
         // ...
         \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, // Add this line
     ],
     // ...
 ];
  1. Run Migrations: Run the database migrations to create the necessary tables for email verification:
Bash
 php artisan migrate
  1. Customize Email Templates (Optional): If you want to customize the email templates for email verification, you can publish them using the following command:
Bash
 php artisan vendor:publish --tag=laravel-mail

This command will copy the email templates to the resources/views/vendor/mail directory, where you can modify them as needed.

  1. Start the Development Server: Start the Laravel development server:
Bash
 php artisan serve

Your application should now be running, and users will be prompted to verify their email addresses upon registration. Laravel will send a verification link to the user’s email address, and they can click on it to confirm their email.

That’s it! You’ve set up email verification in a Laravel 5.8 application. Users will now need to verify their email addresses before gaining access to protected parts of your application.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS