Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MartinGacheru's avatar

laravel admin and superadmin login

I have been trying to fix this problem but to no luck. I have an Agent Model where I want have normal agents and super agents. The normal agents can register benefactors while the super agents can register the agents and also view the normal agent's benefactors. I want to separate the two such that the normal agent cannot access the dashboard of the super agent. So i have created A super agent middleware as follows:

{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
      if (!Auth::check()) {
           return redirect()->route('agents.signin');
       }

        if(Auth::user()->systemAdmin == 0){
          return redirect()->route('benefactors.dashboard');
        }

        if (Auth::user()->systemAdmin == 1) {
          return redirect()->route('agents.dashboard');
        }
    }
}

my AgentLoginController as follows:

class AgentLoginController extends Controller
{

  /*
      |--------------------------------------------------------------------------
      | Login Controller
      |--------------------------------------------------------------------------
      |
      | This controller handles authenticating users for the application and
      | redirecting them to your home screen. The controller uses a trait
      | to conveniently provide its functionality to your applications.
      |
      */

      use AuthenticatesUsers;

      /**
       * Where to redirect users after login.
       *
       * @var string
       */
      protected $redirectTo = '/benefactorDashboard';

      /**
       * Create a new controller instance.
       *
       * @return void
       */


      public function login(Request $request)
      {
          $input = $request->all();

          $this->validate($request, [
              'phoneNumber' => 'required',
              'password' => 'required',
          ]);

          if(auth()->attempt(array('phoneNumber' => $input['phoneNumber'], 'password' => $input['password'])))
          {
              if (auth()->user()->is_admin == 1) {
                  return redirect()->route('agents.dashboard');
              }else{
                  return redirect()->route('benefactors.dashboard');
              }
          }else{
              return redirect()->route('agents.signin')
                  ->with('error','Phone Number or Password Are Wrong.');
          }

      }
  }

I keep getting *Too may redirects * :(

is there a way I can fix this ? :)

here is my agents Table

        Schema::create('agents', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('surname');
            $table->string('firstName');
            $table->string('secondName');
            $table->string('idNumber')->unique();
            $table->string('phoneNumber');
            $table->string('email')->nullable();
            $table->string('password');
            $table->boolean('systemAdmin')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }
0 likes
5 replies
MartinGacheru's avatar

@jlrdw still getting the same error too many redirects


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

  }

jlrdw's avatar
   public function handle($request, Closure $next)
    {
      if (!Auth::check()) {
           return redirect()->route('agents.signin');
       }

           return $next($request);
    }

Use your browser developer tools to help troubleshoot.

You may also need some other stuff from trait duplicated.

1 like
a4ashraf's avatar

@martingacheru

here will be your Middleware

public function handle($request, Closure $next)
{
      if (!Auth::check()) {
           return redirect()->route('agents.signin')->with('error','Phone Number or Password Are Wrong.');;
       }

       return $next($request);
}

and following code for your controller


if(auth()->attempt(array('phoneNumber' => $input['phoneNumber'], 'password' => $input['password']))) {

    $adminType = auth()->user()->is_admin;

    $redirectRoute = ($adminType == 1) ? 'agents.dashboard' : 'benefactors.dashboard';
    
    return redirect()->route($redirectRoute);
} 

jlrdw's avatar

You may not even need the middleware, a redirect back is handled in the trait, the methods can can be copied over as needed.

I just tested without middleware, worked.

Please or to participate in this conversation.