Cannot Login when Using Middleware Laravel 7

Viewed 1036

I had a problem with my Laravel Authentication. I cannot redirect to the page that supposed to go when I'm logging in. Hence, it would redirect back again to my login page when I'm using middleware. I am using middleware because it is a multiple authentication type.

Web.php Routes

Auth::routes();

Route::group(['middleware' => 'auth'], function() {
    Route::get('/', 'HomeController@index')->name('home');
    Route::get('/inbox', 'HomeController@nonadmin')->middleware('admin')->name('inbox');

    //Route::get('/department', 'DepartmentController@index');
    Route::resource('department', 'DepartmentController');
    Route::resource('admin', 'AdminController');
    Route::resource('faculty', 'FacultyController');
    Route::resource('student', 'StudentController');
    //Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
    Auth::logout();
});

Admin.php middleware


namespace App\Http\Middleware;

use Closure;
use Auth; 

class Admin
{
   /**
       * 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('login');
       }

       if(Auth::user()->user_type == 'Admin' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('dashboard');
       }
       
       if(Auth::user()->user_type == 'Staff' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('dashboard');
       }

       if(Auth::user()->user_type == 'Faculty' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('inbox');
       }
       
       if(Auth::user()->user_type == 'Student' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('inbox');
       }

       if(Auth::user()->user_type == 'Parent' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('inbox');
       }
   }
}

LoginController

use AuthenticatesUsers;

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

/**
    * Create a new controller instance.
    *
    * @return void
    */
public function __construct()
{
    $this->middleware('guest')->except('logout');
}

public function logout(Request $request) {
    Auth::logout();
    return redirect('/');
}

public function username()
{
    return 'username';
}

I also declare the middleware in Kernel.php I use this on my previous projects. But on this current one where I use Laravel 7 the current version, it all redirects on a login page.

Update:

I updated the routes as suggestion by Qirel.

Auth::routes();
Route::group(['middleware' => 'auth'], function() {
    Route::get('/', 'HomeController@index')->middleware('admin')->name('home');
    Route::get('/inbox', 'HomeController@nonadmin')->name('inbox');

    //Route::get('/department', 'DepartmentController@index');
    Route::resource('department', 'DepartmentController');
    Route::resource('admin', 'AdminController');
    Route::resource('faculty', 'FacultyController');
    Route::resource('student', 'StudentController');
});

And I also put the return $next($request); on the middleware on the last part still not working. I wondering that in my current project, it is not working but on the past it is working.

2 Answers

In your routes, you've got

Auth::logout();

..which it appears you've added because you think it adds the logout-route for you. That method actually will attempt to log the user out! - it does not add any routes. What currently happens is that whenever any user is authenticated, it will render the routes in that group, and then log them out, thereby making it impossible to log in.

Your solution is simply to remove Auth::logout() from your routes.

To add the logout route explicitly, you need to add

Auth::routes(['only' => 'logout']);

However, you don't need to do that - as you already included all the auth-routes by adding Auth::routes() at the top, which handles it all for you.

Step-1

You write below function in your admin.php middleware

public function handle($request, Closure $next)
   {
       if (!Auth::check()) {
           return redirect()->route('login');
       }

       if(Auth::user()->user_type == 'Admin' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('dashboard');
       }
       
       if(Auth::user()->user_type == 'Staff' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('dashboard');
       }

       if(Auth::user()->user_type == 'Faculty' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('inbox');
       }
       
       if(Auth::user()->user_type == 'Student' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('inbox');
       }

       if(Auth::user()->user_type == 'Parent' &&  Auth::user()->status == 'Active')
       {
           return redirect()->route('inbox');
       }
   }

So it will not woek becuase at same time it check login+role+status so when any one need to login so it has not active status.

SO Please Replace that function with below code

public function handle($request, Closure $next)
   {
       if (!Auth::check()) {
           return redirect()->route('login');
       }

       if(Auth::user()->user_type == 'Admin')
       {
           return redirect()->route('dashboard');
       }
       
       if(Auth::user()->user_type == 'Staff' )
       {
           return redirect()->route('dashboard');
       }

       if(Auth::user()->user_type == 'Faculty')
       {
           return redirect()->route('inbox');
       }
       
       if(Auth::user()->user_type == 'Student')
       {
           return redirect()->route('inbox');
       }

       if(Auth::user()->user_type == 'Parent' )
       {
           return redirect()->route('inbox');
       }
   }

Step-2

In your logincontroller.php you write,

   public function logout(Request $request) {
    Auth::logout();
    return redirect('/');
}

public function __construct()
{
    $this->middleware('guest')->except('logout');
}

so please remove that function and paste below line in logincontroller.php

public function __construct()
    {
        $this->middleware('auth');   
    }

Now it will redirect to the '/' route or the route you wanted.

Related