0

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 App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Facades\Auth;

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
"Tymon\JWTAuth\JWTGuard"

AuthController.php

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

public function login(Request $request){
    $credentials = $request->only('email', 'password');

    if(!$token = $this->guard()->attempt($credentials)){
        return response()->json(['error' => "L'email ou le mot de passe ne correspondent pas."]);
    }else {
        return response()->json([
            'token' => $token,
        ]);
    }
}
public function guard()
{
    return Auth::guard();
}

And Multiple function me,logout...

Kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    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',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

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

}

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 !

EDIT :

api.php

Route::group(['prefix' => 'auth', 'namespace' => 'Auth'], function ($router) {
    Route::post('/inscription', 'AuthController@register');
    Route::post('/connexion', 'AuthController@login');
    Route::get('/logout', 'AuthController@logout');
    Route::get('/me', 'AuthController@me');
    Route::get('/isAdmin', 'AuthController@isAdmin');
});

Solution not found :(

19
  • 2
    add your route file .. did you use it ? Commented Sep 25, 2020 at 8:23
  • Yes, i didn't add because my route work fine all my function in my controller work too. Commented Sep 25, 2020 at 8:27
  • please add route file also... it is not possible to explain by comment... Commented Sep 25, 2020 at 8:35
  • 2
    I have add api.php route file in bottom of the post. You talk to other file ?? Commented Sep 25, 2020 at 8:39
  • 2
    Let us continue this discussion in chat. Commented Sep 25, 2020 at 9:18

1 Answer 1

1

Authenticate.php

public function handle($request, Closure $next, ...$guards)
{
    $this->authenticate($request, $guards);

    $user = $this->auth->user();

    ...do your thing....

Kernel.php

protected $middlewareGroups = [
    'api' => [
        \Illuminate\Session\Middleware\StartSession::class,
        \AEPlanning\Http\Middleware\VerifyCsrfToken::class,

You will get user only after successful login

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.