Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$request->user() returns null in Laravel 5.5

I'm having issues with getting the user from an Illuminate Request ($request->user()).

 $request->user() // whatever i do this keeps returning null

The PusherBroadcastDriver uses this and i can't edit third party code for obvious reasons..

I'm using Tymon\JWT to generate tokens and authenticate users with my application. I currently tried to do the following (check code)

AUTHENTICATION SERVICE PROVIDER:

public function boot()
{
    $this->registerPolicies();

    Auth::extend('jwt', function ($app, $name, array $config) {
        return new JWTGuard($app['tymon.jwt'],
            Auth::createUserProvider($config['provider']),
            $app['request']);
    });

}

MIDDLEWARE:

 public class JWTRefreshMiddleware extends RefreshToken
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request $request
 * @param  \Closure $next
 *
 * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
 *
 * @return mixed
 */
public function handle($request, Closure $next)
{
    $response = parent::handle($request, $next);
    $this->addUserToRequest();
    return $response;
}

private function addUserToRequest()
{
    $request = app('request');
    $user = User::find(JWTAuth::getPayload()['user_id']);
    $request->merge(['user' => $user]);
    $request->setUserResolver(function () use ($user) {
        return $user;
    });
    Auth::setUser($user);
}

}

But unfortunately the above didn't work. Can someone point me in the right direction?

like image 277
Arre Devious Avatar asked Oct 18 '22 05:10

Arre Devious


2 Answers

This is the final solution:

 class JWTRefreshMiddleware extends RefreshToken
 {
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request $request
 * @param  \Closure $next
 *
 * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
 *
 * @return mixed
 */
public function handle($request, Closure $next)
{
    $response = parent::handle($this->addUserToRequest($request), $next);
    return $response;
}

private function addUserToRequest(\Illuminate\Http\Request $request): \Illuminate\Http\Request
{
    $user = User::find(JWTAuth::manager()->decode(JWTAuth::getToken())['user_id']);
    $request->merge(['user' => $user]);
    $request->setUserResolver(function () use ($user) {
        return $user;
    });
    Auth::setUser($user);

    return $request;
  }
 }
like image 122
Arre Devious Avatar answered Nov 15 '22 06:11

Arre Devious


It seems you are changing a variable, not the request instance, maybe you can try this.

public function handle($request, Closure $next)
{
    $response = parent::handle($this->addUserToRequest($request), $next);

    return $response;
}

private function addUserToRequest($request)
{
    $user = User::find(JWTAuth::getPayload()['user_id']);
    $request->merge(['user' => $user]);
    $request->setUserResolver(function () use ($user) {
        return $user;
    });
    Auth::setUser($user);
    return $request;
}
like image 31
Hanlin Wang Avatar answered Nov 15 '22 05:11

Hanlin Wang