Laravel-modules: L5.4 default Auth middleware dosen't work

Created on 20 Apr 2017  路  11Comments  路  Source: nWidart/laravel-modules

File: /Modules/News/Http/routes.php

Route::group([
    'namespace' => 'Modules\News\Http\Controllers\Admin',
    'prefix' => 'admin',
    'as' => 'admin.',
    'middleware' => ['auth', 'force_locale']
], function () {

    Route::get('/news', ['as' => 'news.index', function () {
        dd(123);
    }]);

});

In main route if I check Auth::check() (user logged in) it return true, in module/routes it always false. Where is the my mistake?

Most helpful comment

Finally I found the problem. In documentation:

Out of the box, the web middleware group is automatically applied to your routes/web.php file by the RouteServiceProvider.

In web middleware start sessions and if I add 'web' moddleware to my module routes group all work fine:

# Modules/News/Http/routes.php
Route::group(['middleware' => ['web','auth'], 'prefix' => 'news', 'namespace' => 'Modules\News\Http\Controllers'], function()
{
    Route::get('/', 'NewsController@index')->name('admin.news.index');
});

All 11 comments

My custom 'force_locale' middleware (in app/http/middlewre/..) work fine.

Possibly the session / middleware haven't ran yet when the route file is loaded.

Middleware ran but Auth::check() always false!

If it's true in your controller it's something to do with your setup.

@nWidart it's true in /app/Http/Controllers but false in /Modules/Http/Controllers. All other functionality of Module package work fine.

# routes/web.php
...
Route::get('/', function () {
    dd(Auth::check()); # return true
    return view('home');
});
...
# Modules/News/Http/routes.php
...
Route::get('/news', ['as' => 'news.index', function () {
    dd(Auth::check()); # return false
}]);
...

Module News was enabled

Maybe someone can check this in issue in self code?

In Modules/News/Http/Controllers/NewsController.php Auth::check(); return true.
Can't understand why 'auth' middleware return false

In this way middleware 'auth' work:

Route::group(['middleware' => 'web', 'prefix' => 'news', 'namespace' => 'Modules\News\Http\Controllers'], function()
{
    Route::get('/', 'NewsController@index')->name('admin.news.index')->middleware('auth');
});

In this dosen't:

Route::group(['middleware' => 'auth', 'prefix' => 'news', 'namespace' => 'Modules\News\Http\Controllers'], function()
{
    Route::get('/', 'NewsController@index')->name('admin.news.index');
});

Finally I found the problem. In documentation:

Out of the box, the web middleware group is automatically applied to your routes/web.php file by the RouteServiceProvider.

In web middleware start sessions and if I add 'web' moddleware to my module routes group all work fine:

# Modules/News/Http/routes.php
Route::group(['middleware' => ['web','auth'], 'prefix' => 'news', 'namespace' => 'Modules\News\Http\Controllers'], function()
{
    Route::get('/', 'NewsController@index')->name('admin.news.index');
});

@elephantux i just wanna thank you veeery mush. this problem make me headache for 2 day. and it finally solved now T^T /

Was this page helpful?
0 / 5 - 0 ratings

Related issues

githubmarau picture githubmarau  路  29Comments

bashet picture bashet  路  19Comments

designvoid picture designvoid  路  10Comments

n4p4 picture n4p4  路  17Comments

zcwilt picture zcwilt  路  11Comments