Skip to content

Instantly share code, notes, and snippets.

@arcanedev-maroc
Forked from nasrulhazim/readme.md
Created February 1, 2017 21:22
Show Gist options
  • Select an option

  • Save arcanedev-maroc/0ab07a30865c49b26e1444d934e99c8e to your computer and use it in GitHub Desktop.

Select an option

Save arcanedev-maroc/0ab07a30865c49b26e1444d934e99c8e to your computer and use it in GitHub Desktop.

Revisions

  1. @nasrulhazim nasrulhazim revised this gist Jan 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    Before proceed, you may want to follow up

    1. [Send Welcome Email Notification with Event and Listener](https://gist.github.com/nasrulhazim/10ffa2cfafcaaf54784db6e566b5a7d3) and
    2. [Enable Account Activation in Order to Login to the System](https://gist.github.com/nasrulhazim/10ffa2cfafcaaf54784db6e566b5a7d3)
    2. [Enable Account Activation in Order to Login to the System](https://gist.github.com/nasrulhazim/febeec1b417096fc68b62eebc47f37c6)

    # Step 1

  2. @nasrulhazim nasrulhazim revised this gist Jan 24, 2017. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -151,4 +151,24 @@ Add a new Blade Template named `auth/activation.blade.php`
    </div>
    </div>
    @endsection
    ```
    ```

    # Step 7

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

    ```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.
  3. @nasrulhazim nasrulhazim created this gist Jan 24, 2017.
    154 changes: 154 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,154 @@
    Before proceed, you may want to follow up

    1. [Send Welcome Email Notification with Event and Listener](https://gist.github.com/nasrulhazim/10ffa2cfafcaaf54784db6e566b5a7d3) and
    2. [Enable Account Activation in Order to Login to the System](https://gist.github.com/nasrulhazim/10ffa2cfafcaaf54784db6e566b5a7d3)

    # Step 1

    Create a middleware

    ```
    php artisan make:middleware CheckActivationStatus
    ```

    # Step 2

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

    ```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

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

    # Step 4

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

    ```php
    <?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)
    {
    // dd(auth()->user()->activated_at);
    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:

    ```php
    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`

    ```html
    @extends('layouts.app')

    @section('content')
    <div class="container">
    <div class="row">
    <div class="col-md-8 col-md-offset-2">
    <div class="panel panel-default">
    <div class="panel-heading">Activation Account</div>
    <div class="panel-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
    ```