1/24/2017 - 5:17 AM

Using Middleware to Check Account Activation and Resend Activation E-mail

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()

Step 4

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


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)) {
            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;

        // send notification
            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


<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">
                        <div class="alert alert-warning">{{ $notice }}</div>
                    <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>

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

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

Step 7

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



Step 8

Now you may test the login using any accounts.