Passport Returns An Empty User For Authenticated User

Posted 2 years ago by ZetecVan

I'm creating an internal API that needs to know if a user is authenticated or not. I'm using Laravel 5.4 and Spark 4.0. I've installed Passport as per the docs.

There is a video I found (https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/13) where Taylor explains Passport. About 3/4 through he explains about consuming your own API for authenticated users. I've followed that, but am getting an empty response from the return $request->user() part, even though I'm logged in.

Here's some code:

Route::group([
    'middleware' => 'auth:api'
], function () {

     Route::get('/user', function() {
         return['username' => 'test'];
     });
});

Route::get('/user', function (Request $request) {
    return $request->user();
});

So it's not authenticating via the middleware auth:api, and it's hitting the /user outside the middleware closure.

In my vuejs component I have

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        this.$http.get('/api/user')
            .then(response => {
                console.log(response.data);
            });

        },
        ready(){
        }
    }
</script>

and in my App.js I have

Vue.http.headers.common['Accept'] = 'application/json';
Vue.http.headers.common['X-CSRF-TOKEN'] = Spark.csrfToken;

I can see through Chrome Devtools that there is a Cookie:laravel_token on the GET /api/user request as explained in the video, but my reponse.data is empty.

Can anyone help?

Please sign in or create an account to participate in this conversation.