mardi 23 avril 2019

Specify custom middlewareexecution order in Laravel 5.8

I have a custom middleware that identifies a tenant in the application. When I call the endpoint without 'auth' middleware it identifies the tenant correctly and can read the tables as required. However I need auth to execute after the tenant has been identified (i.e so the correct db is queried for the auth user) I can't seem to see where I have gone wrong with my configuration. I get an error that says:

Undefined index: database

but this is because my identifyTenant middleware has not run to set the database at the point auth middleware tries to authenticate.

Route group

Route::group(['middleware' => ['tenant', 'auth', 'manager_admin']], function () {

Route::resource('invoice',  'InvoiceController');});

Priority middleware section of kernel.php as suggested here in docs

 protected $middlewarePriority = [
    // --- Added identify m.ware at the top to run first.
    \App\Http\Middleware\IdentifyTenant::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\Authenticate::class,
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Illuminate\Auth\Middleware\Authorize::class,
];

Route middleware section of kernel.php

protected $routeMiddleware = [
    'tenant'         => \App\Http\Middleware\IdentifyTenant::class,
    '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,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];

I am using laravel 5.8.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire