我在Laravel中获得Auth :: user为空 [英] I get Auth::user is null in Laravel

查看:1060
本文介绍了我在Laravel中获得Auth :: user为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码为管理员角色创建中间件:

I create middleware for an admin role using the following code:

php artisan make:middleware AdminMiddleware

然后,我为登录页面创建一条路由:

After that, I create a route for the login page:

Route::get('admin/login', ['middleware'=>'web','as'=>'admin.login','uses'=>'AdminController@loginView']);
Route::post('admin/login',['middleware'=>'web','as'=>'admin.login','uses'=>'AdminController@login']);
Route::group(['prefix'=>'admin','middleware' => ['auth.admin','web']],     function()
{
    Route::get('/', ['as'=>'admin.home','uses'=>'AdminController@index']);
    Route::get('/home',    ['as'=>'admin.home','uses'=>'AdminController@index']);
});

控制器是

class AdminController extends Controller
{
    //
    function index(){
        return 'welcome';
    }

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

    function login(Request $request){
        $error = $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:5',
        ]);
        $email = $request->input('email');
        $password = $request->input('password');
        $remember = $request->input('remember');

        if (Auth::attempt(['email' => $email, 'password' => $password,'type'=>'admin'], $remember)) {

            // Authentication passed...
            Auth::login(Auth::user(), $remember);
            return redirect()->route('admin.home');
        }
        else{//('message', 'Login Failed')
            return redirect()->route('admin.login')->withErrors($request->all(), "message")->withInput();
        }
    }
}

在AdminMiddleware中

And in AdminMiddleware

public function handle($request, Closure $next)
{
    var_dump(Auth::user());
    if(!Auth::check()){
        return redirect()->route('admin.login')->withErrors('You are not logged in');
    }
    elseif ($request->user()->type != 'admin'){
        dd($request->user());
        return redirect()->route('admin.login')->withErrors('You have not authority');
    }
    return $next($request);
}

错误是:对于AdminMiddleware中的每个$ request-> user()或Auth:user,我总会得到null.

The error is: I always get null for each $request->user() or Auth:user in AdminMiddleware.

推荐答案

您将中间件以不正确的顺序传递给路由组.

You're passing the middleware to the route group in an incorrect order.

现在您具有以下顺序['auth.admin', 'web'],这意味着auth.admin中间件将在web组中的中间件之前执行,并且由于web包含StartSession中间件,因此您无需auth.admin中获取已认证用户所需的任何会话.

Right now you have this order ['auth.admin', 'web'] which means that the auth.admin middleware will be executed before the middleware from the web group, and since web contains the StartSession middleware, you won't have any session in auth.admin which is needed to get the authenticated user.

因此只需像这样切换中间件顺序:

So simply switch the middleware order like so:

Route::group(['prefix'=>'admin','middleware' => ['web', 'auth.admin']], function () {
    // now the session is set up in `web` and then you have it in `auth.admin`
});

这篇关于我在Laravel中获得Auth :: user为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆