I am having trouble with setting up routes for a controller that is in a package and uses the auth middleware.
I have the following in the package ServiceProvider:
public function boot()
{
$package_dir = __DIR__ .'/../../';
$this->loadRoutesFrom($package_dir .'routes/web.php');
$this->loadViewsFrom($package_dir .'resources/views', 'core');
}
In the package's own routes/web.php:
Route::resource('/users', '\Ocelot\Core\Http\Controllers\UsersController')->middleware('auth');
//Route::get('/users', '\Ocelot\Core\Http\Controllers\UsersController@index')->middleware('auth');
//Route::get('/users', [\Ocelot\Core\Http\Controllers\UsersController::class, 'index'])->middleware('auth');
Neither of these variants work, until I remove ->middleware('auth').
The same applies if I set the middleware in the controller constructor:
public function __construct()
{
$this->middleware('auth');
}
What happens is that when I visit /users I get the following trace
- /users 302 redirect to ...
- /login 302 redirect to ...
- /home
However, if I put Route::resource('/users', '\Ocelot\Core\Http\Controllers\UsersController')->middleware('auth');into the application root routes/web.php the exact same code works as expected.
I am on Laravel version 6.9.0.
Please also note...
- I use a custom table name for the users table
- My User class is also in the package and not in App\User
- The whole time I am not logged out or something - the session is stable
For some reason, the Illuminate\Auth\Middleware\Authenticate::authenticate() method thinks I am not authorized and dd's a Null here
protected function authenticate($request, array $guards)
{
if (empty($guards)) {
$guards = [null];
}
foreach ($guards as $guard) {
if ($this->auth->guard($guard)->check()) {
return $this->auth->shouldUse($guard);
}
}
dd(\Illuminate\Support\Facades\Auth::user());
$this->unauthenticated($request, $guards);
}
The Http Kernel is untouched since install:
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
/**
* The priority-sorted list of middleware.
*
* This forces non-global middleware to always be in the given order.
*
* @var array
*/
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\Authenticate::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
];
}
Am I missing something or have I discovered a bug? :)
Related: https://laracasts.com/discuss/channels/code-review/package-controller-not-called-when-using-middlewareauth-while-signed-in