2

I have a small dilema, i'm trying to make a login system that differentiates normal users from admin users using the laravel auth scaffolding.

The problem is it goes in a infinite redirect loop in the middleware.

After I press the login button it constantly redirects to a route and the question is, how can I solve this issue the "laravel way" or any other way for that matter.

Here are my controllers: 1. The basic home controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
   /**
   * Show the application dashboard.
   *
   * @return \Illuminate\Http\Response
   */
  public function index()
  {
    return view('home');
  }
}
  1. The main admin controller - entry controller:

     namespace App\Http\Controllers\Admin;
    
     use Illuminate\Http\Request;
     use App\Http\Controllers\Controller;
    
     class Start extends Controller
     {
    
      public function index(){
    
        return view('admin/index');
      }
    
    }
    
  2. Login Controller(the default one from the auth scaffolding- modified by me, I removed the constructor):

        namespace App\Http\Controllers\Auth;
    
        use App\Http\Controllers\Controller;
        use Illuminate\Foundation\Auth\AuthenticatesUsers;
    
        class LoginController extends Controller
        {
          use AuthenticatesUsers;
          protected $redirectTo = '/home';
        }
    
  3. The Middleware(redirect if RedirectIfAuthenticated):

            namespace App\Http\Middleware;
    
            use Closure;
            use Illuminate\Support\Facades\Auth;
    
            class RedirectIfAuthenticated
            {
               public function handle($request, Closure $next, $guard = null)
              {
                if (Auth::guard($guard)->check()) {
                  if(Auth::user()->type == 2){//if user type is 1 then it's an admin.
                   return redirect()->route('web-admin');
                  }else{
                    return redirect()->route('home');
                 }
              }
            return $next($request);
          }
        }
    
  4. The route file(web routes)

       Route::get('/', function () {
         return view('index');
       });
    
       Auth::routes();
       Route::middleware(['auth','guest'])->group(function() {
       Route::get('home',['as'=>'home', 'uses'=>'HomeController@index']);
       Route::get('web-admin',['as'=>'web-admin', 'uses'=>'Admin\Start@index']);
      });
    

2 Answers 2

1

The guest/RedirectIfAuthenticated redirects any request to corresponding home route for authenticated users. The problem is that admin home route is behind this middleware as well, that's why it keeps redirecting to the same page.

You need to remove the guest middleware from the route group - it should only be applied to routes that should be available to unauthenticated users only.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Removing guest middleware from login route stops redirect. Thanks.
0

For sure this is an infinite loop because you applied both guest and auth middle ware to your routes, so also authenticated users will be redirected and this is an infinite loop.

Keep the RedirectIfAuthenticated.php as its original code and redirect authenticated users inside your main controller based on their type:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
    * Show the application dashboard.
    *
    * @return \Illuminate\Http\Response
    */
    public function index(Request $request)
    {
        // if type 1 is admin, why did you redirect type 2 to web-admin?!
        if($request->user()->type == 2) { //if user type is 1 then it's an admin.
            return redirect()->route('web-admin');
        }
        return view('home');
    }
}

You may do same redirection in your admin controller for normal users to redirect them back in case they try to access admin page.

Additionally modify web.php routes as following:

Route::get('/', function () {
    if(auth()->user()->type == 2) { //if user type is 1 then it's an admin.
        return redirect()->route('web-admin');
    } else {
        return redirect()->route('home');
    }
})->middleware('auth');

Auth::routes();
Route::middleware('auth')->group(function() {
    Route::get('home',['as'=>'home', 'uses'=>'HomeController@index']);
    Route::get('web-admin',['as'=>'web-admin', 'uses'=>'Admin\Start@index']);
});

3 Comments

it was a typo regarding users type, type 2 is the admin
This way you'll end up having multiple redirects. First to home from the middleware, then to web-admin from HomeController.
Nope. Middleware just redirect authenticated users to "/" in case they try to access login page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.