Laravel RedirectifAuthenticated不能与多个自定义防护一起使用

时间:2019-09-02 21:27:06

标签: laravel

我有两个认证保护adminusercustomer
我编辑了RedirectIfAuthenticated中间件,但是当我以客户身份登录时,它将重定向到为经过身份验证的管理员设置的页面(即,管理仪表盘视图)。
但是我需要在登录后将客户重定向回首页。

为什么客户登录名重定向到管理仪表板?

这是RedirectifAuthenticated中间件

public function handle($request, Closure $next, $guard = null)
{
    if ($guard === "adminuser" && Auth::guard($guard)->check()) {
            return redirect('/adminhome');
    }
    if ($guard === "customer" && Auth::guard($guard)->check()) {
        return redirect('/customerhome');
    }
    if (Auth::guard($guard)->check()) {
            return redirect('/customerhome');
    }
    return $next($request);
}

LoginController.php

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

// login for the Admin user
public function userLogin(Request $request)
{
    $this->validate($request, [
            'username'   => 'required|min:3',
            'password' => 'required|min:6|max:6'
    ]);
    $status = Auth::guard('adminuser')->attempt(['email' => $request->username, 'password' => $request->password]);
    if ($status == true) {
            return redirect()->intended('/adminhome');
    } else {
            $request->session()->flash('error', 'Sorry,Login Unuccessfull!');
            return redirect()->back();
    }
}

// login for the customer
public function customerLogin(Request $request)
{
    $this->validate($request, [
            'username' => 'required|min:3',
            'password' => 'required|min:6|max:6'
    ]);

    $status = Auth::guard('customer')->attempt(['email' => $request->username, 'password' => $request->password], $request->get('remember'));
    if ($status == true) {
            return redirect()->intended('/customerhome');
    } else {
            return redirect()->intended('/customerhome');
    }
}

这是路线

Auth::routes();

Route::get('/index', 'FrontController@index')->name('index');
Route::get('/admin/login', 'adminloginController@ShowAdminLoginForm') > name('adminLogin');  //route for admin login page

Route::post('/register', 'FrontController@storeRegister');

Route::post('/payment', 'PaymentController@processpayment');

Route::post('/customerLogin', 'Auth\LoginController@customerLogin');
Route::post('/adminUserLogin', 'Auth\LoginController@userLogin');

Route::view('/adminhome', 'home');
Route::view('/customerhome', 'index');

0 个答案:

没有答案