1
votes

I can't get the information of the authenticated user in a Laravel passport app with JWT and vue.

I've installed laravel passport. Ive done everything in the documentation and added:

\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,

To consume it with js for a SPA app.

I've protected my routes with the auth:api middleware, but i keep getting:

{"Status":{"api_status":0,"Code":401,"Message":"Unauthenticated"}}

When i use postman to manually insert the CSRF-TOKEN in Authorization Bearer Token. It does give me the auth user.

Whatever i do, i keep getting null on Auth::user(); in my controllers and routes

Laravel V5.7 Node V10.15.3 Npm V.6.9.0

1

1 Answers

0
votes

You need to send a POST request (using Postman/Insomnia) with the details of the user you want to log in as to /oauth/token in your app which will respond with an API token. You save this api token locally, and then add add it as a Header variable to your guzzle/axios/whatever function's http calls (every one of them!) to your API.

$http = new GuzzleHttp\Client;
$data = 'Whatever';
$response = $http->request('POST','api/user',[
    'data' => $data,
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer xxxxxxxxxxxxThis is the long authentication string returned from the Postman requestxxxxxxxxxxxxxx'
    ]
]);
dd(json_decode((string) $response->getBody())); // To view the response

From: https://laravel.com/docs/5.5/passport#creating-a-password-grant-client