atnic/laravel-email-verification

Laravel Email Verification Scaffolding

v0.1.4 2018-08-29 04:23 UTC

This package is auto-updated.

Last update: 2024-10-08 09:39:47 UTC


README

Implementation

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 to User model.
<?php
...
use Atnic\EmailVerification\Traits\EmailVerifiable;
class User extends Authenticatable
{
    use EmailVerifiable;
    ...
  • In app/Http/Kernel.php, modify $routeMiddleware property, change auth middleware
<?php
...
class Kernel extends HttpKernel
{
    ...
    protected $routeMiddleware = [
        // 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth' => \Atnic\EmailVerification\Http\Middleware\Authenticate::class,
        ...
  • Override registered() method on RegisterController
<?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!

Advance Usage

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.

Security Vulnerabilities

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.

License

Laravel Email Verification is open-sourced package licensed under the MIT license.