In this post, I am going to explained email verification in laravel 7. How to use email varification in laravel 7, laravel 7 auth verify or email auth verify, you can understand a concept of laravel 7 auth verify email. I explained simply step by step laravel 7 auth verify email. Let’s see bellow example laravel 7 authentication email verification.
Laravel 7 provides email verification setup for new registered users to must have to verify his email verification before proceed. but in laravel old version if we need to email verification process then that we are doing email verification process manually.
You just need to make some basic setup with need to use middleware, routes and mail configuration.
Just follow steps and you will set up for email verification in laravel 7 project.
Step 1: Install Laravel 7
In first step, we need to install Laravel 7 version application using bellow command, So open your terminal OR command prompt and run bellow command:
1 2 3 | composer create-project --prefer-dist laravel/laravel blog |
Step 2: Database Configuration
In this step, we need to configure database configuration details on .env file. So let’s create username, password etc. So let’s add.
.env
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | APP_URL=http://localhost:8000 DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel7 DB_USERNAME=root DB_PASSWORD= |
After database configuration, you need to run default migration of laravel by following command:
1 2 3 | php artisan migrate |
Step 3: Email Configuration
Here, we need to add email configuration in .env file. We are sending email after user registration so we need to add email smtp details for send email.
.env
1 2 3 4 5 6 7 8 9 10 11 12 13 | MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=youremail@gmail.com MAIL_PASSWORD=yourpass MAIL_ENCRYPTION=tls |
Step 4: Create Auth
You have to follow few step to make auth in your laravel 7 application.
First you need to install laravel/ui package as like bellow:
1 2 3 | composer require laravel/ui |
Now you can use to create registration, login and forgot password with routes by auth command, So simply run bellow command to create:
1 2 3 | php artisan ui bootstrap --auth |
Now you need to run npm command, otherwise you can not see better layout of login and register page.
Install NPM:
1 2 3 4 5 6 7 | npm install Run NPM: npm run dev |
Step 5: Email Verification Setup
In this step, we need to add email verification setup, so basically we have to add email verification class implement in user model, use middleware for protection. So you need to update like as bellow files one by one:
app/User.php
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 31 32 | <?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', ]; } |
routes/web.php
1 2 3 4 5 6 7 8 9 | Route::get('/', function () { return view('welcome'); }); Auth::routes(['verify' => true]); Route::get('/home', 'HomeController@index')->name('home'); |
app/Http/Controllers/HomeController.php
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\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'); } } |
Now you are ready to run your laravel 7 app. So let’s check by following command:
1 2 3 | php artisan serve |
I hope you found your best…
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.
Leave a Reply