October: [L5.5] Auth facades not working on routes and CMS pages

Created on 22 Aug 2017  路  3Comments  路  Source: octobercms/october

Expected behavior

BackendAuth::getUser() return logged user when I call it from my plugin routes.php file, like in build 419

Actual behavior

BackendAuth::getUser() return null (same behavior for Auth::getUser()).

Reproduce steps
  1. Add following route to your plugin
Route::get('/test', function() {
    dd(
        Auth::getUser(),
        BackendAuth::getUser()
    );
});
  1. connect to backend and or site frontend
  2. go to /test

Same behavior if you add the dump inside a CMS page but work fine from plugins onRun and init method.

I have followed the error and seems this come from the following lines
https://github.com/octobercms/library/blob/develop/src/Auth/Manager.php#L340-L345

Cookie::get() return a string instead of an array like expected by the test on line 343.

October build

420

duplicate

Most helpful comment

Thanks @LukeTowers

For those that have the same problem :

Route::get('/test', function () {
    dd(
        Auth::getUser(),
        BackendAuth::getUser()
    );
})->middleware('web');

All 3 comments

Add the web middleware to your route and it will work. Session handling and other related middleware components are no longer run on every single route by default in Laravel 5.5 so you need to add the web middleware to your routes for it to work. There are some related issues to this with the RainLab.Translate plugin. See https://github.com/rainlab/translate-plugin/issues/288

Thanks @LukeTowers

For those that have the same problem :

Route::get('/test', function () {
    dd(
        Auth::getUser(),
        BackendAuth::getUser()
    );
})->middleware('web');
Was this page helpful?
0 / 5 - 0 ratings