我在这里遵循了指南:https ://laravel.com/docs/5.4/passport#sumption-your-api-with-javascript
使用 axios:
...
mounted: function() {
axios.get('/api/user')
.then(function (response) {
console.log(response)
})
.catch(function (response) {
console.error(response);
});
},
但是响应总是未经身份验证,我检查是否存在 laravel_token cookie,它是:
我在 apache2 ( docker ) 上运行
- - 更新 -
调试后,它实际上是在此方法中失败的 xsrf 令牌TokenGuard
:
/**
* Authenticate the incoming request via the token cookie.
*
* @param Request $request
* @return mixed
*/
protected function authenticateViaCookie($request)
{
try {
$token = $this->decodeJwtTokenCookie($request);
} catch (Exception $e) {
return;
}
# This is not passing:
if (! $this->validCsrf($token, $request) ||
time() >= $token['expiry']) {
return;
}
if ($user = $this->provider->retrieveById($token['sub'])) {
return $user->withAccessToken(new TransientToken);
}
}
我在 boostrap.js 中有适当的设置:
window.axios = require('axios');
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest'
};