@iamjaredsimpson Set your axios defaults to what the documentation suggests.
window.axios = require('axios');
window.axios.defaults.withCredentials = true;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am working on an SPA that will need to communicate with a browser extension. Sanctum is working correctly when using the SPA authentication to access the api, but I can't seem to get it to authenticate when using the token.
I have created a new route to check the login details from the extension and either generate a token or pass back the previously generated token for a specific browser. This appears to be working correctly.
public function token(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required',
'device_name' => 'required'
]);
$user = User::where('email', $request->email)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
if(!$user->tokens()->where('name', $request->device_name)->first()) {
return $user->createToken($request->device_name)->plainTextToken;
}
return $user->plainTextToken($request->device_name);
}
I'm then immediately sending an axios request with the returned token. Here is a simplified version of my Axios request in the extension:
axios.defaults.headers.common['Authorization'] = `bearer ${token}`;
axios.defaults.headers.common['Accept'] = 'application/json';
axios.get('http://site.localhost/api/files/index')
.then(response => {
this.files = response.data;
});
Route in routes/api.php:
Route::middleware('auth:sanctum')->get('/files/index', 'Api\FileController@index');
But for whatever reason, I can't seem to get this to authenticate correctly, despite the SPA version of sanctum working fine. Does anyone have any suggestions?
Please or to participate in this conversation.