私のコードは、クッキーとして応答するトークンを生成します。axiosを使用してクッキートークンを送信するにはどうすればよいですか?
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
});
ここで、ユーザーリストを取得するためにトークンを送信する方法に問題があります。私はwithCredentialsを追加しようとしました。本当ですが、運はありません。
token_not_providedという応答エラーが発生しました。
質問:
- 応答からクッキー・トークンの値を取得する方法は?
- 承認ベアラ{トークン}を使用してAxiosを使用してトークンを送信する方法はありますか?
ありがとうございます。
コードの画像をアップロードしないでください。誰もあなたが簡単にコピー貼り付けできるコードを再入力する時間がありません – milo526