21
JulLaravel supports verifying email addresses out of the box. This package adds support for verifying new email addresses. When a user updates its email address, it won’t replace the old one until the new one is verified. Super easy to set up, still fully customizable. If you want it can be used as a drop-in replacement for the built-in Email Verification features as this package supports unauthenticated verification and auto-login. Support for Laravel 6.0 and higher and requires PHP 7.3 or higher.
You can install the package via composer:
composer require protonemedia/laravel-verify-new-email
Publish the database migration, config file and email view:
php artisan vendor:publish --provider="ProtoneMedia\LaravelVerifyNewEmail\ServiceProvider"
You can set the redirect path in the verify-new-email.php
config file. The user will be redirected to this path after verification.
Add the MustVerifyNewEmail
trait to your User model and make sure it implements the framework’s MustVerifyEmail
interface.
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use ProtoneMedia\LaravelVerifyNewEmail\MustVerifyNewEmail;
class User extends Authenticatable implements MustVerifyEmail
{
use MustVerifyNewEmail, Notifiable;
}
Now your User model has a few new methods:
// generates a token and sends a verification mail to 'me@newcompany.com'.
$user->newEmail('me@newcompany.com');
// returns the currently pending email address that needs to be verified.
$user->getPendingEmail();
// resends the verification mail for 'me@newcompany.com'.
$user->resendPendingEmailVerificationMail();
// deletes the pending email address
$user->clearPendingEmail();
The newEmail
method doesn’t update the user, its current email address stays current until the new one if verified. It stores a token (associated with the user and new email address) in the pending_user_emails
table. Once the user verifies the email address by clicking the link in the mail, the user model will be updated and the token will be removed from the pending_user_emails
table.
The resendPendingEmailVerificationMail
does the same, it just grabs the new email address from the previous attempt.
The user that verified their email address will be logged in automatically. You can disable this by changing the login_after_verification
configuration setting to false.
The default Laravel implementation requires the user to be logged in before it can verify its email address. If you want to use this package’s logic to handle that first verification flow as well, override the sendEmailVerificationNotification
method as shown below.
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use ProtoneMedia\LaravelVerifyNewEmail\MustVerifyNewEmail;
class User extends Authenticatable implements MustVerifyEmail
{
use MustVerifyNewEmail, Notifiable;
public function sendEmailVerificationNotification()
{
$this->newEmail($this->getEmailForVerification());
}
}
For more details, Please visit Github.
If you are a business and want to leverage Laravel for your next PHP-based web application, CRM Software, E-commerce application, SAAS application, or any custom web application development for your custom requirements, you must discover an amazing team with experience in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Please feel free to send us an email at rizwan@codebrisk.com or get in touch with us, our business person will get back to you.
Published at : 21-07-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project