In this tutorial you will learn about the Laravel 7/6 Email Verification Tutorial Example and its application with practical example.
In this Laravel 7/6 Email Verification Tutorial I will show you how to implement email verification for newly registered users in laravel. In this tutorial you will learn to enable email verification for account activation in laravel.
Laravel provides built-in email verification system for newly registered user. Using this, newly registered user will get an email with an account activation link. This activation link is used for account verification. When activation link is clicked, this will make user account verified and active for the application. Once user account is activated then it will be allowed to access protected routes; which can be accessible only by verified accounts.
Laravel 7/6 Email Verification Tutorial Example
In this step by step tutorial, we’ll show you how to setup email verification system for newly registered user in laravel.
- Install Laravel Fresh Setup
- Setup Database and SMTP Detail
- Create Auth Scaffolding
- Migrate Database
- Add Route
- Add Middleware In Controller
- Run Development Server
- Conclusion
1. Install Laravel Fresh Setup
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
2. Setup Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Now we will setup SMTP credential in .env to send emails. We are using mailtrap SMTP credential in this example.
1 2 3 4 5 6 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=your_mailtrap_user_name MAIL_PASSWORD=your_mailtrap_user_password MAIL_ENCRYPTION=tls |
3. Create Auth Scaffolding
Now we need to enable authentication scaffolding in laravel.
4. Migrate Database
Now, go to app/providers/AppServiceProvider.php and put the below code :
1 2 3 4 5 6 |
use Illuminate\Support\Facades\Schema; function boot() { Schema::defaultStringLength(191); } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
After migrating the database tables we need to add the MustVerifyEmail Contracts in App/User.php. Open the App/User.php file add the constructor like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements MustVerifyEmail { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; } |
5. Add Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 |
Route::get('/', function () { return view('welcome'); }); //first Auth::routes(); //after Auth::routes(['verify' => true]); |
6. Add Middleware In Controller
We need to add Middleware in the controller constructor. Go to app/controllers/HomeController
and put this line $this->middleware([‘auth’, ‘verified’]); inside of constructor :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware(['auth', 'verified']); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index() { return view('home'); } } |
7. Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/ |