Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Middleware to Check Account Activation and Resend Activation E-mail

Before proceed, you may want to follow up

  1. Send Welcome Email Notification with Event and Listener and
  2. Enable Account Activation in Order to Login to the System

Step 1

Create a middleware

php artisan make:middleware CheckActivationStatus

Step 2

Then register the middleware in app\Http\Kernel.php

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'active' => \App\Http\Middleware\CheckActivationStatus::class,
];

Step 3

Setup middleware in app\Http\Controllers\HomeController.php in it's constructor

public function __construct()
{
  $this->middleware(['auth','active']);
}

Step 4

Open up app\Http\Middleware\CheckActivationStatus.php and add the following:

<?php

namespace App\Http\Middleware;

use Closure;

class CheckActivationStatus
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (empty(auth()->user()->activated_at)) {
            auth()->logout();
            return redirect()->route('account.activation.request');
        }
        return $next($request);
    }
}

Step 5

Update your ActivationController by adding two more methods below:

    public function request()
    {
        return view('auth.activation');
    }

    public function resend()
    {
        $this->validate(request(), [
            'email' => 'required',
        ]);

        $user = User::where('email', request('email'))->first();
        $token = str_random(64);
        $user->activation_token = $token;
        $user->save();

        // send notification
        $user->notify(
            new SendActivationEmail($token)
        );

        return redirect()->route('account.activation.request')->with('notice', 'Please check your email for activation link');
    }

Step 6

Add a new Blade Template named auth/activation.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Account Activation') }}</div>

                <div class="card-body">
                    @if(isset($notice))
                        <div class="alert alert-warning">{{ $notice }}</div>
                    @endif
                    <form class="form-horizontal" role="form" method="POST" action="{{ route('account.activation.resend') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-8 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Resend Activation E-mail
                                </button>

                                <a class="btn btn-link" href="{{ url('/password/reset') }}">
                                    Forgot Your Password?
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Step 7

Lastly, you need to add the route for Activation request and activation request submission handler in routes/web.php

Route::get(
    'account/activation/request',
    'Auth\ActivationController@request'
)->name('account.activation.request');

Route::post(
    'account/resend/activation',
    'Auth\ActivationController@resend'
)->name('account.activation.resend');

Step 8

Now you may test the login using any accounts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.