2

I'm trying to change the header of my request before passing it to the controller using a middleware but it seems $next($request) executes the code in my controller. Is there a way to change the header then send the updated request to my controller?

My middleware:

class JWTAuthenticator
{

public function handle($request, Closure $next)
{   
    $token =JWTAuth::getToken();
    $my_new_token = JWTAuth::refresh($token);
    //it runs here
    $response = $next($request);

    //it runs this part after executing the controller  
    $response->header('Authorization','Bearer '.$my_new_token);
    return $response;
}

This is how the middleware is assigned to my route:

Route::get('/{user}', 'v1\UserController@find')->middleware('jwt_auth');

1 Answer 1

4

That way you are excecuting the $response->header('Authorization','Bearer '.$my_new_token); sentence after the request was attended. Change your code as follows:

class JWTAuthenticator
{

public function handle($request, Closure $next)
{   
    $token =JWTAuth::getToken();
    $my_new_token = JWTAuth::refresh($token);

    $request->headers->set('Authorization','Bearer '.$my_new_token);

    return $next($request);
}
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.