Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Majeed's avatar

How to set Bearer access_token in request header from Laravel Back-end

I'm try this way to add Bearer access_token in every request header from my middleware.

 public function handle($request, Closure $next)
    {
        $request->header('Authorization',"Bearer ".$request->bearerToken());

        return $next($request);
    }

But it's not working. Is there any way ??

0 likes
4 replies
Nee's avatar

Hi Majeed, is there a reason why you would want to set the access token in the backend. My understanding is that the bearer token is used to check if a request is authenticated before allowing access to a protected route. If it is preset in the backend, it defeats the purpose of trying to authenticate the request.(unless I’m wrong)

Secondly, you are trying to access a bearer token from the $request object, when the token is not being passed in the first place.

1 like
Majeed's avatar

I want this because when I send a request from browser to an API endpoints. where Postman isn't present. that situation how can i handle this Bearer Token. Please you have any solutions that's kind of situation suggest me. I'm new working with API. Thank You @Nee

Nee's avatar

I'm sure you've fixed the problem by now.

You should pass the bearer token along with your request to the backend.

For instance, when making an HTTP call from the front end, you can include the

bearer token in your header or body, depending on what the backend requires.

I don't think it is a good idea to set that in a middleware on the backend itself.

Sorry for the late reply @majeed.

martinbean's avatar

@majeed You pass the Bearer token when issuing the request. You might want to have a look at Passport if you’re trying to use your own API in an application.

If you’re trying to automatically add a Bearer token via middleware, then there’s no point having authentication in your API at all.

1 like

Please or to participate in this conversation.