简体   繁体   English

处理管理员和用户身份验证-Laravel

[英]Handling Admin and User Authentication - Laravel

I have 2 two users (Admin and operators) for my system and i want to authenticate them to their various pages based on their roles. 我的系统有2个两个用户(管理员和操作员),我想根据其角色对他们的各个页面进行身份验证。 I am using the Authenticated.php middleware to achieve this job like below 我正在使用Authenticated.php中间件来完成此工作,如下所示

but i get an error when trying to login with any of the users as 但是尝试以任何用户身份登录时出现错误

Call to undefined method Illuminate\\Contracts\\Auth\\Factory::check()

What am i doing wrong please? 请问我做错了什么?

Authenticated.php Authenticated.php

public function handle($request, Closure $next, ...$guards)
{
    if(Auth::check()) {
        if(Auth::user()->hasRole('administrator')) {
            return redirect('/');
        } else if (Auth::user()->hasRole('operator')) {
            return redirect('client/dashboard');
       }
    }
    // $this->authenticate($guards);
    return $next($request);
}

Route.php Route.php

Route::group(['middleware' => ['auth']], function () {
    Route::get('/', 'PagesController@dashboard');
});

Route::group(array('prefix' => 'client', 'namespace' => 'User', 'middleware' => ['auth']), function () {
    Route::get('/dashboard', 'DashboardController@create');
});

Aren't you messing up with your if condition? 您不是搞砸了您的if条件吗? Try the below code in your RedirectIfAuthenticated.php file in App\\Http\\Middleware. 在App \\ Http \\ Middleware的RedirectIfAuthenticated.php文件中尝试以下代码。 Hope that will resolve your problem. 希望能解决您的问题。

public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            if(Auth::user()->hasRole('administrator'))
            {
                return redirect('/');

            }

            else
            {
                return redirect('client/dashboard');
            }
        }

        return $next($request);
    }

And Are you using Entrust for handling roles? 并且您是否正在使用Entrust来处理角色?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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