Laravel Email Verification is a package for generate scaffolding code for email verification within just few step.
But before that make sure you already config you .env
especially for MAIL_*
keys. I prefer you to set up using mailtrap.io, its super easy!
Okay, then this is the step to install Laravel Email Verification:
- Install this package
composer require atnic/laravel-email-verification
- Make sure you're already run
php artisan make:auth
before, then run
php artisan make:email-verification
- Run migration to add
email_verified
column to users table
php artisan migrate
- Add
Atnic\EmailVerification\Traits\EmailVerifiable
trait toUser
model.
<?php
...
use Atnic\EmailVerification\Traits\EmailVerifiable;
class User extends Authenticatable
{
use EmailVerifiable;
...
- In
app/Http/Kernel.php
, modify$routeMiddleware
property, changeauth
middleware
<?php
...
class Kernel extends HttpKernel
{
...
protected $routeMiddleware = [
// 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth' => \Atnic\EmailVerification\Http\Middleware\Authenticate::class,
...
- Override
registered()
method onRegisterController
<?php
...
use Atnic\EmailVerification\Notifications\EmailVerification;
class RegisterController extends Controller
{
...
/**
* The user has been registered.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function registered($request, $user)
{
$user->notify(new EmailVerification($user->generateEmailVerificationUrl()));
if ($user->isEmailVerificationTimeoutExpired()) {
auth()->logout();
return response()->redirectToRoute('verify_email.resend', [ 'email' => $user->email ])->with('status', __('email-verification::verify_email.link_sent'));
}
}
...
- Done!
In User
model you can add $emailVerificationTimeout
using integer value that define how much time in minutes user can logged in even when their email is unverified yet. You can notify them that their email is not verified and can click this link to resend email verfication. route('verify_email.resend', [ 'email' => 'some_email' ])
. The default is 0, so user can't be login if email is not verified.
If you discover a security vulnerability within Laravel Email Verification, please send an e-mail to Farid Inawan via frdteknikelektro@gmail.com. All security vulnerabilities will be promptly addressed.
Laravel Email Verification is open-sourced package licensed under the MIT license.