如何使用Laravel API CSRF令牌在请求Vue.js上生成身份验证令牌

How do I get the Auth token from a Laravel API and immediately use that token in an http/ajax/axios request?

JS / Client

Vue.axios.post('http://laravelApi:8000/here', {someData}).then(response->{ 
  console.log(response.data); 
});

PHP / API

route::post('/here', function(){
  response...
});

Is there a favoured pattern for returning & re-posting the csrf-token? Can I define the headers differently between an initial request and subsequent requests that include the cors token?

Could I store the laravel_sesson &/or XSRF-TOKEN cookie from the first response and the include it with subsequent responses. Can anyone point me towards good resources or code examples?

I keep finding examples from laravel with vue deployed bundled on a single server/host and would like to find clarification on async implentation of csrf tokens. Thanks!

Edit: Lots of good answers around for this one, but not so many at the time of posting. CSRF token doesn't need to be generated at request-time but instead persists throughout session.

duanqiang5722
duanqiang5722 谢谢!我在考虑使用JWTAuth。我最近在Laravel-Angular项目中使用它:laravel-angular.io/#由于Laravel生成了csrf令牌并使用我做的第一个api请求返回它并认为我只需要存储该csrf令牌并将其包含在其他要求。只是没有100%明确如何获得/设置它。并且希望在没有用户需要进行身份验证/登录的情况下使其工作。
3 年多之前 回复
douman6245
douman6245 toolong
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐