mercredi 1 novembre 2017

Custom Middleware - 'Too Many Redirects' - Laravel

I want to create a custom middleware that only if the user is authenticated and the email is a certain email to access the /admin page.

Although, when I specify my custom route and then a redirect it always says too many redirects..

Short Explanation.

  1. User Logs in -> redirected to /home. (Works)
  2. If user tries to access /admin and their email isn't like the one specified in the middleware, redirect to /home.
  3. If its true, let them in /admin

My middleware is called 'admin.verify'

Routes File:

Route::get('/admin', 'AdminController@index')->name('admin.index');

AdminController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function __construct(){
      $this->middleware(['auth', 'admin.verify']);
    }


    public function index(){
      return view('admin.test');
    }
}

Middleware:

 public function handle($request, Closure $next)
    {

      if (Auth::check() && Auth::User()->email == 'Tester@gmail.com') {
        return $next($request);
      } else {
        return redirect()->route('home');
      }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ilmIlk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire