I create api token without passport.
First I added column api_token
to user
table. Next when user logged in I create new api token and save this token in DB.
Next I added to api.php
code:
Route::group(['middleware' => 'auth:api'], function() {
Route::post('action', 'APIController@action');
});
Next I using axios to send data:
const api_token = '<?php echo $api_token ?>';
let options = {
method: 'post',
url: '/api/action',
data: data,
headers: {
"X-CSRFToken": document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
}
let params = {
'api_token': api_token
}
axios(options,params)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Next in AuthServiceProvider.php
I added:
public function boot()
{
$this->registerPolicies();
Auth::extend('token', function ($app, $name, array $config) {
return new TokenGuard(Auth::createUserProvider($config['provider']), $app->request);
});
}
I using this article. Still I have error:
401 (Unauthorized)
EDIT:
I change my axios:
const api_token = '<?php echo $api_token ?>';
let options = {
method: 'post',
url: '/api/action',
data: mergedData,
headers: {
"X-CSRFToken": document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
},
params: {
'api_token': api_token
}
}
axios(options)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Now I send token in GET, but I still have error:
http://127.0.0.1:8000/api/action?api_token=b005413b-3197-48ac-8747-32bbcf8b858f 401 (Unauthorized)