mardi 24 septembre 2019

How do I make my bearer token available on all requests in Laravel?

This question is based on API auth with laravel Passport. I know the access token can be retrieved with:

$accessToken = $request->bearerToken();
Or
$accessToken = $request->header('Authorization')

Or generated with:

$accessToken = $user->createToken('MyApp')->accessToken;

But generating it like above and setting it like this:

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

is only retrievable in the next request only but not used in the header as expected as the next request will not work if it expects a token and it was not set explicitly in the headers option in Postman.

Every tutorial I have seen always tell you to do

'headers' => [
  'Accept' => 'application/json',
  'Authorization' => 'Bearer '.$accessToken,
]

while this works within Postman, we don't use the token in Postman forever since it's just for testing, we ought to use it in an actual app.

So, how do I set the token to be able to access every endpoint that requires it successfully?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire