Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.7 email verification expiration time

I would like to customize the time users have to verify their email address that happens through the built in Auth (since 5.7).

In config/auth there is:

'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

But I haven't found anything similar for email verification. There is also no mention in the official documentation.

like image 377
eskimo Avatar asked Dec 05 '18 15:12

eskimo


People also ask

Why do verification links expire?

Most validation links simply contain some secret that they send out your way, only in the possession of which may you verify the email address. The reason they changed the code is because it probably expires. In that case you could not activate the account, so they sent you another in case you'd like to continue.

What is the latest version of Laravel email verification?

Moving ahead, with the release of Laravel 5.7 the user email verification is shipping with the framework out of the box. People were creating the custom feature to implement this before the version 5.7.

How to verify the ownership of an email in Laravel?

Email Verification after Registration with Laravel 5.7 Nowadays most of the modern applications force their users to verify the ownership by sending an activation email after they complete the account registration. Moving ahead, with the release of Laravel 5.7 the user email verification is shipping with the framework out of the box.

What is mustverifyemail in Laravel?

It is new in Laravel 5.7 as the verification functionality is implemented in this version. Implement mustVerify interface in the User model. In the User.php model, you can see one more contract added called MustVerifyEmail. To use the email verification process, we need to implement this contract.

How to create a verification controller in Laravel?

Go to the routes >> web.php file and add the extra parameter inside Auth::routes (). This enables the new Verification controller with the route actions. You can see the new controller called VerificationController. php file already comes with Laravel 5.7.


3 Answers

Whilst the question specifically addresses Laravel 5.7, I feel that it is worth mentioning that as of Laravel 5.8, it is possible to achieve this with a config variable. My search for customising the verification expiration time returned this question as the top result, hence my addition.

If we check out Illuminate\Auth\Notifications\VerifyEmail, the verificationUrl method now looks like this:

protected function verificationUrl($notifiable)
{
    return URL::temporarySignedRoute(
        'verification.verify',
        Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
        ['id' => $notifiable->getKey()]
    );
}

As such, we can just add this block to config/auth.php to customise the time without needing to extend the classes or anything:

'verification' => [
    'expire' => 525600, // One year - enter as many mintues as you would like here
],

UPDATE: I've written about the above approach, as well as another on customising the process by overiding the verificationUrl method to give you more flexibility, on my blog.

like image 150
James Avatar answered Oct 17 '22 15:10

James


In deed the options is not there in Laravel, but since laravel makes use of the following:

  • a trait MustVerifyEmail (in Illuminate\Foundation\Auth\User class extended by the main User model)

  • Event and Notification

In the MustVerifyEmail trait, there's a method called sendEmailVerificationNotification. This is where the Notification VerifyEmail class referenced by @nakov's answer and its function verificationUrl is used:

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    $this->notify(new Notifications\VerifyEmail);
}

Since we know this, we can do the following:

  • Extend the Notifications\VerifyEmail to our custom VerifyEmail class
  • override the implementation of verificationUrl
  • override the implementation of the sendEmailVerificationNotification method in the User model to use our new VerifyEmail class.

Having done the above, our User model will have the following method:

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    $this->notify(new \App\Services\Verification\VerifyEmail);
}

Now we make use of our custom VerifyEmail class. Then our new VerifyEmail class would look like this:

namespace App\Services\Verification;

use Illuminate\Support\Carbon;
use \Illuminate\Support\Facades\URL;

class VerifyEmail extends \Illuminate\Auth\Notifications\VerifyEmail
{
    protected function verificationUrl($notifiable)
    {
        return URL::temporarySignedRoute(
            'verification.verify', Carbon::now()->addMinute(3), ['id' => $notifiable->getKey()]
        );  //we use 3 minutes expiry
    }
}

Well, apart from the explanations, the process is quite straight forward. I hope it is easy to grasp. Cheers!

like image 7
Oluwatobi Samuel Omisakin Avatar answered Oct 17 '22 16:10

Oluwatobi Samuel Omisakin


If you open the Illuminate\Auth\Notifications\VerifyEmail::class;

The method that generates the URL already uses expiration time which defaults to 1 hour. Unfortunately there is no option to modify that value.

/**
 * Get the verification URL for the given notifiable.
 *
 * @param  mixed  $notifiable
 * @return string
 */
protected function verificationUrl($notifiable)
{
    return URL::temporarySignedRoute(
        'verification.verify', Carbon::now()->addMinutes(60), ['id' => $notifiable->getKey()]
    );
}
like image 4
nakov Avatar answered Oct 17 '22 15:10

nakov