Hello I amm trying to secure API with laravel passport, but always throws me 401 unauthorized. where am i making a mistake? Any help? Thank you.
I added into User.php HasApiTokens,
and uncomment 'App\Model' => 'App\Policies\ModelPolicy'
and added to boot Passport:routes() in AutServiceProvider.php,
in
auth.php 'api' => [
'driver' => 'passport',]
in RegisterController.php
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$token = $user->createToken('API Token')->accessToken;
return $user;
}
in bootstrap.js
const token = '3acdc8cddb433947ea7cc51de909fdc50dcdf601ac521ac37ff1b3ee3a61e47e2111d297a07e147e'
window.axios = require('axios');
window.axios.defaults.withCredentials = true;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
window.axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
in Posts.vue axios
mounted() {
axios.get('/public/api/posts').then(response=>{
this.data =response.data
})
},