Laravel get Bearer token from a request

Panjeh
Panjeh
Jun 5 · 3 min read

I wrote this article as a complementary of the article about Laravel call route internally.

A- Get Laravel Bearer token:

Take a look at this link in Laravel 5.8.

As you see, there is a bearerToken() method on the Illuminate\Http\Request object, which gets the bearer token from the request headers. (bearerToken() is available from Laravel version 5.2).

If you look at the Request class, you see that it is using InteractsWithInput Trait:

The logic behind this is in line 53 of InteractsWithInput Trait as follow:

   if (Str::startsWith($header, 'Bearer ')) {
return Str::substr($header, 7);
}
}

So you should just invoke this method to get the bearer token:

And then get what you expect.

B- Set Laravel Bearer Token:

If you want to set a Bearer Token, you should follow this according to the Laravel document:

Your application’s API consumers may provide their API token as a Bearer token in the Authorization header of the request:

Read more:

If you want to know more about bearer token read these answers.


Thank you for reading! If you enjoyed this article:

Clap it ! Share it! Follow Me in Medium!

Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.

Have a very wonderful day!

Previous Stories You will Love:

Panjeh

Written by

Panjeh

Web geek, Self-taught full-stack web developer, Learning Laravel, Vuejs, UX/UI design, Physicist PhD