kilroyweb / email-verification by kilroyweb

Support for verifying user email
72
1
1
Package Data
Maintainer Username: kilroyweb
Package Create Date: 2017-06-28
Package Last Update: 2017-06-28
Language: PHP
License: MIT
Last Refreshed: 2024-11-21 03:01:13
Package Statistics
Total Downloads: 72
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

Installation

composer require kilroyweb/email-verification

Add to the $routeMiddleware array in app/Http/Kernel.php

'verified_email' => \KilroyWeb\EmailVerification\Middleware\RequireVerifiedEmail::class,
'non_verified_email' => \KilroyWeb\EmailVerification\Middleware\RequireNonVerifiedEmail::class,

Add the HasEmailVerification trait to your User class

use \KilroyWeb\EmailVerification\Traits\HasEmailVerification;

Routes

Use the supplied "verified_email" or "non_verified_email" middleware

Route::namespace('Email')->prefix('/email')->group(function(){
    Route::namespace('Verification')->prefix('/verification')->group(function(){
        Route::get('/create', 'VerificationController@create')->middleware(['auth','non_verified_email']);
        Route::get('/{token}', 'VerificationController@show');
    });
});

Controllers

A controller is easy to whip up using available methods inherited from the HasEmailVerification trait:

<?php

namespace App\Http\Controllers\Email\Verification;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class VerificationController extends Controller
{

    public function create(){
        $user = \Auth::user();
        $user->requestEmailVerification();
        return redirect('/account')->withSuccess('Verification email sent!');

    }

    public function show($token){
        $user = \App\User::findByEmailVerificationToken($token);
        if(!$user){
            abort(404);
        }
        $user->verifyEmail();
        \Auth::login($user);
        return redirect('/account')->withSuccess('Your email address has been verified!');
    }

}

Create a mailable (app/Mail/EmailVerification.php) that is triggered during "requestEmailVerification()"

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class EmailVerification extends Mailable
{
    use Queueable, SerializesModels;

    public $verifiableModel;

    public function __construct($verifiableModel)
    {
        $this->verifiableModel = $verifiableModel;
    }

    /**
     *
     *Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $verifiableModel = $this->verifiableModel;
        $this->to($verifiableModel->email);
        return $this->view('email.email-verification.create',[
            'verifiableModel'=>$verifiableModel,
        ]);
    }
}

And view (email.email-verification.create):

@extends('layouts.email.app')

@section('content')

    <h2>Click here to verify your email address:</h2>

    <p><a href="{{ url('/email/verification/'.$verifiableModel->getEmailVerificationToken()) }}">Verify My Email</a></p>

@endsection

In your register controller, call the requestEmailVerification() method to send verification on register:

protected function create(array $data)
    {
        $user = User::create([
            ...
        ]);
        $user->requestEmailVerification();
        return $user;
    }