[Vuejs]-Laravel user api_token is undefined

0👍

If you are trying to consume your own api from vuejs there’s no need to set the api token manually. Just update the web middleware group in app/Http/Kernel.php to include this line:

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

This middleware will attach a laravel_token cookie that contains an encrypted JWT that Passport will use to authenticate API requests from your JavaScript application.

Read more here: https://laravel.com/docs/5.6/passport#personal-access-tokens

But, if you are consuming this same api from an external source like a mobile app, an api token will be required by passport to authenticate the request. The token can be created when the user is logged in or registered. Here’s how:

//create a token

$token = $user->createToken('Token Name')->accessToken;

Then add an headers object to axios when making a request to the api

axios({
  method: 'method',
  url: 'url',
  headers: {
    'Accept' => 'application/json',
    'Authorization' => 'Bearer '.$token
  }
})
.then()
.catch()

Read more here: https://laravel.com/docs/5.6/passport#managing-personal-access-tokens

Leave a comment