The latest version of laravel was just released and there are some major changes in the framework. In this article, we will discuss the email verification after registration in laravel. If you want to know about all the features and upgrades in laravel 5.7, you can read this article.
Laravel is always a hot topic with frequent upgrades and active community among the PHP developers. In the previous version of laravel authentication, there was no activation feature. We have to manually add the activation after registration. But, with this version of laravel, email verification comes by default.
We will discuss the implementation in this article.
Create Project
Since laravel was just released, you may not be able to get the latest version of laravel. If you are unable to install laravel 5.7, you can still install it via dev branch. Run the command:
composer create-project laravel/laravel blog --prefer-dist // install before release composer create-project laravel/laravel blog --prefer-dist --dev-develop
Authentication Scaffolding
One of the best feature in laravel is that it comes with default authentication. Simply running a single artisan command create all the required routes, controller and view files. To create a authentication scaffolding, run the artisan command:
php artisan make:auth
Model and Migration
In the previous version of laravel also, we get User
model and respective migrations file. But, in laravel 5.7, there is a slight change in the migration file as email verification field was added. Email verification is optional. Which means we can implement email verification or not that depends on our needs. When we open the migration file, we can see below code:
//database/migrations/..create_users_table.php public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
Similarly, when we go through our model, we also see a slight change in it. There is a new interface called MustVerifyEmail
. By default, User model doesn’t implement this interface. This is a simple interface with three functions.
In our case, we are going to implement it. Thus our User model looks like below:
Routing
We can define if we want to implement email verification or not through routing. For email verification, we must pass
verify
through route. So, our authentication route looks as below:Auth::routes(['verify' => true]);Protecting Routes
We can also protect our routes for verified account only by using middleware. To do so, we attach our middleware to respective routes as below:
Route::get('reports', function () { // Only verified users may enter... })->middleware('verified');Controller
To handle the necessary verfication process and other stuff, a new controller called
VerificationController
has been added. This controller usesVerifiesEmails
trait, that does the magic. This trait has 2 functions and below is a screenshot of this trait.user()->hasVerifiedEmail() ? redirect($this->redirectPath()) : view('auth.verify'); } /** * Mark the authenticated user's email address as verified. */ public function verify(Request $request) { if ($request->route('id') == $request->user()->getKey()) { $request->user()->markEmailAsVerified(); } return redirect($this->redirectPath()); } /** * Resend the email verification notification. */ public function resend(Request $request) { $request->user()->sendEmailVerificationNotification(); return back()->with('resent', true); } }Views
After we run artisan command to create authentication, laravel automatically creates necessary view files under
resources/view/auth
directory.Redirect After verfication
When a user is successfully authenticated, our system will automaticallyrecirect to the
/home
URI. But, we can customize it according to our need. We can redirect to other by defining aredirectTo
attribute on theVerificationController
protected $redirectTo = '/to-your-route';Conclusion
This is it for implementation of email verification in laravel 5.7. This is one of the most requested features in the upgrade from laravel 5.6. Thank you for reading this article. If you have any comment or feedback, feel free to drop a comment below.