Laravel middleware Auth work in controller but not in my middleware

  authentication, laravel, middleware, vue.js

i’m in trouble for 2 days with the laravel auth middlewares.

I have understand how the Auth system work with JWTtoken. In my controller all work fine i got my user login and i can access with Auth::guard->user().

But i have one problem in my middleware Authenticate.php :

<?php

namespace AppHttpMiddleware;

use IlluminateAuthMiddlewareAuthenticate as Middleware;
use IlluminateSupportFacadesAuth;

use Closure;

class Authenticate extends Middleware
{

    protected function redirectTo($request)
    {   dd(
            $request->user(),
            auth()->id() ?? '?',
            Auth::id() ?? '?',
            $request->user()->id ?? '?',
            auth()->check(),
            get_class(auth()->guard())
        );
    }

}

I try everything i found in multiple post or tutorial, but the result is the same (same time in controller all work user is login) :

null
"?"
"?"
"?"
false
"TymonJWTAuthJWTGuard"

AuthController.php

public function __construct()
    {
        $this->middleware('auth', ['except' => ['login', 'register']]);
    }
And Multiple function login,me,logout...

Kernel.php

<?php

namespace AppHttp;

use IlluminateFoundationHttpKernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middleware = [
        // AppHttpMiddlewareTrustHosts::class,
        AppHttpMiddlewareTrustProxies::class,
        FruitcakeCorsHandleCors::class,
        AppHttpMiddlewareCheckForMaintenanceMode::class,
        IlluminateFoundationHttpMiddlewareValidatePostSize::class,
        AppHttpMiddlewareTrimStrings::class,
        IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            // IlluminateSessionMiddlewareAuthenticateSession::class,
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],

        'api' => [
            //'throttle:60,1',
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],
    ];

    protected $routeMiddleware = [
        'auth' => AppHttpMiddlewareAuthenticate::class,
        'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
        'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
        'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class,
        'can' => IlluminateAuthMiddlewareAuthorize::class,
        'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
        'password.confirm' => IlluminateAuthMiddlewareRequirePassword::class,
        'signed' => IlluminateRoutingMiddlewareValidateSignature::class,
        'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
        'verified' => IlluminateAuthMiddlewareEnsureEmailIsVerified::class,
    ];

}

I have try to take rules in $middlewareGroups web and past in $middleware, and if i do that is say :
"Call to a member function parameters() on null".

So nothing change my Auth is null.

Thanks a lot if someone can explain me the problem and what i do wrong !

Source: Laravel

Leave a Reply