简体   繁体   中英

How to redirect to admin page with middleware auth in Laravel 5.4?

I am struggling with this for couple of days, and I can't find what I am doing wrong.

I created a login form. The routes are under group middleware auth. When I try to login it shows me that I am logged, but I am not redirected to my admin panel, I am returning to the login page. I'm pretty sure that the problem is with the middleware but I don't know what I am dong wrong.

My routes:

Route::get('/admin/login','Admin\LoginController@index')->name('login');
Route::post('/admin/loginF','Admin\LoginController@loginF');

Route::group(['middleware' => 'auth'], function()
{
   Route::get('/admin','Admin\DashboardController@index')->name('dashboard');
   Route::get('/admin/logout',function(){
     Auth::logout();
    return redirect('/admin/login/');
   });
});

Login: class LoginController extends Controller {

use AuthenticatesUsers;

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

public function Index() {
    return view('admin.login');
}

public function loginF(Request $request) {

     $credentials = [
        'username' => $request['username'],
        'password' => $request['password'],
    ];

   if (Auth::attempt($credentials)) {
       return redirect("/admin");
   } else {   
   return Redirect::back()->withErrors("Error Login!");
   }
}
}

And in my DashboardController I have

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

EDIT:

This is the default, didn't change it

  class RedirectIfAuthenticated{
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
           return redirect('/home');
        }
        return $next($request);
   }
 }

I remember what i did in my case. I created a middleware and in which i checked the $guard variable.

$this->middleware('LoggedIn:admin'); //is called in constructor of MyAdmin with which all other admin pages were extended.

Middleware

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check() == false) {
          switch ($guard) {
            case 'client':
              return redirect('/Login');
              break;
            case 'vendor':
              return redirect('/Login');
              break;
            case 'employee':
              return redirect('/Login');
              break;
            case 'admin':
              $output = $this->EmployeeAccess($request);
              if($output['status'] == false){
                return redirect($output['link']);
              }
              break;
            default:
              return redirect('/');
              break;
          }
        }

        return $next($request);
    }

On login page you should check

if($this->guard('admin')->check() === true) // then redirect to admin dashboard

for other users

 if($this->guard('user')->check() === true) // then redirect to  user dashboard

On your loginF method:

public function login()
{
    //check if auth passed, if so, redirect to dashboard
    if (Auth::check()) return redirect('/dashboard');

    //if failed, return view to login
    return view('login');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM