skip to Main Content

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' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            // IlluminateSessionMiddlewareAuthenticateSession::class,
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],

        'player' => [
            AppHttpMiddlewareCpanelAuthentication::class,
        ],
    ];

    protected $routeMiddleware = [
        'auth' => IlluminateAuthMiddlewareAuthenticate::class,
        'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
        'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
        'can' => IlluminateAuthMiddlewareAuthorize::class,
        'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
        'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
        'player' => AppHttpMiddlewareCpanelAuthentication::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 !

2

Answers


  1. You’re calling check() which checks to see if a user is authenticated.

    If you want to see if they aren’t logged in, call Auth::guard($guard)->guest() instead.

    Login or Signup to reply.
  2. Middleware:

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

    Route:

    Route::group(['middleware' => ['AppHttpMiddlewareCpanelAuthentication']], function () {
             //routes
    });
    

    Try without registering in kernel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search