Best way to customize user verification in laravel

Best Way To Customize User Verification In Laravel

Hi guys,

We’ll look into Laravel’s Best Way To Customize User Verification. For your application, it is recommended practice to validate users’ email addresses. Laravel has methods for sending and validating email verification requests that are simple to use.

Model Preparation:-

You can import the Illuminate\Contracts\Auth\MustVerifyEmail contract on your user model file for implementing Email verification.

 <?php
 namespace App;
 
 use Illuminate\Contracts\Auth\MustVerifyEmail;
 use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Notifications\Notifiable;

 class User extends Authenticatable implements MustVerifyEmail
 {
    use Notifiable;

    // ....
 }

This interface will trigger the email with a verification link when the user gets registered.

Database Consideration:

Your user table must contain an email_verified_at column to store the DateTime that the email confirmation. For that, you can migrate to the table.

 php artisan migrate

Routing:-

Laravel contains a VerificationController class that contains logic for sending and verifying emails. For that, you must add the option ‘verify’ to the Auth::routes method.

 Auth::routes(['verify' => true]);

protecting routes:

Laravel provides route middleware to allow verified users to access a given route. It already registered this middleware in your application kernel. You need to add middleware to the route definition.

 Route::get('profile', function () {
    // Only verified users may enter…
 })->middleware('verified');

Views:-

You can generate all the necessary views for email verification. Executing the below command:

 composer require laravel/ui

 php artisan ui vue --auth

You can customize the email verification view as your application by changing the resources\views\auth\verify.blade.php file.

After verifying Emails:-

After the email address is confirmed, it automatically redirects the user to /home. You can customize redirection to another location by defining the redirectTo property.

 protected $redirectTo = '/dashboard';

Events:-

Laravel dispatches events during the email verification process. You may attach listeners to these events in your EventServiceProvider.

 /**
  * The event listener mappings for the application.
  *
  * @var array
  *
  */

  protected $listen = [
     'Illuminate\Auth\Events\Verified' => [
          'App\Listeners\LogVerifiedUser',
      ],
  ];

I hope that this post (Best Way To Customize User Verification In Laravel) has helped you understand user verification and how to activate it in a Laravel application. We can also limit the actions and views of people. I used this site to write this post. Please leave a remark if you have any queries, and I will answer as quickly as possible.

Thank you for reading this article. Please share this article. That’s it for the day. Stay Connected!
Cheers,

Loading

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top