Tuesday, February 28, 2017

Laravel New Middleware not working

I have a problem in my project, i just try to fix in many hours but it still not working. I've created a new middleware - here is my code:

class CpanelAuthentication
{
    public function handle($request, Closure $next, $guard = 'player')
    {
        if (Auth::guard($guard)->check()) {
            return redirect('cpanel');
        }
        return $next($request);
    }
}

i'm just config provider and guard too. - here is my auth.php

    'guards' => [
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],

            'api' => [
                'driver' => 'token',
                'provider' => 'users',
            ],

            'player' => [
                'driver' => 'session',
                'provider' => 'player',
            ],
        ],

   'providers' => [
        'users' => [
            'driver' => 'database',
            'table' => 'tbl_users'
        ],

        'player' => [
            'driver' => 'database',
            'table' => 'tbl_player'
        ],
    ],

And i register this middleware in Kernel.php too - Here is my Kernel.php

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',
    ],

    'player' => [
        \App\Http\Middleware\CpanelAuthentication::class,
    ],
];

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'player' => \App\Http\Middleware\CpanelAuthentication::class
];

And finaly i put this middleware in a route group like this:

Route::group(['middleware'  => 'player'], function() {
    Route::group(['prefix' => 'cpanel', 'namespace' => 'Cpanel'], function() {
        Route::get('/', [
            'as'    => 'getCpanel',
            'uses'  => 'CpanelController@getCpanel'
        ]);
        Route::group(['prefix' => 'investment'], function() {
            Route::get('/', [
                'as' => 'getCpanelInvestment',
                'uses' => 'InvestmentController@getCpanelInvestment'
            ]);
        });
    });
});

But the middleware not working. When i try to access the url

cpanel/investment

It's still pass the middleware and redirect inside although the user is not authenticated!

Can anyone help me ! Thanks so much !




via Train Heartnet

Advertisement