0
这是我的代码,用于生成将作为cookie进行响应的令牌。如何使用axios发送cookie标记?
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = ['email'=>$request->header('email'),'password'=>$request->header('password')];
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response($token)->cookie(
'token',$token, 60
);
}
而我正在使用axios来处理它。这是我的代码:
axios({
method: 'post',
url: 'http://test.dev/authenticate',
headers : {
'email':'[email protected]',
'password':'secret'
},
json:true,
})
axios({
method: 'get',
url: 'http://test.dev/api/users',
json:true,
withCredentials: true
});
现在,我在如何发送令牌以检索用户列表时遇到问题。我试着添加Credentials:真实但没有运气。
我收到了响应错误token_not_provided。
问题:
- 如何从响应中的Cookie标记的值?
- 如何使用带有Authorization Bearer {token}的axios发送令牌?
谢谢提前。
不要上传代码图片,如果你想获得帮助上传代码本身的严重镜头!没有人有时间重新键入代码,你可以轻松地复制粘贴 – milo526