Reputation: 463
i created laravel cms using vue and axios.
i want get current user that sending post requests
so i followed laravel api documentation and take this structure
// bootstrap.js
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
window.axios.defaults.headers.common['Authorization'] = `Bearer ${window.api_token}`;
// vue component file
axios.post('/api/v1/person', this.person)
.then(data => {
this.$emit('added', data.data);
this.person.id = data.data.data.id
});
// Route in api.php
Route::prefix('v1')->name('api.')->group(function () {
/** Person Routes */
Route::prefix('person')->namespace('Person')->name('person.')->group(function(){
Route::post('/', 'PersonController@index');
});
});
//in laravel controller i retrun
return response()->json(auth('api')->user());
even i checked console headers and Authorization
header set properly
i can get all post data but laravel don`t pass me the user
also i made a repository of this project in github if you want can follow this link
https://github.com/mohammadZx/crm
Upvotes: 0
Views: 401
Reputation: 463
by default, laravel trying to find user by hashing token and for this reason laravel return null because your user api tokens not hashed. so if you change auth.php setting to don't searching hashes api_tokens, this problem will be fixed
'guards' => [
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false
],
],
Upvotes: -1
Reputation: 18926
In the documentation it states that you have to set the correct guard for Passport to work. Update auth.php
config.
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
Upvotes: 2