Laravel Basic auth:api 401 Unauthorized

时间:2017-09-26 22:43:23

标签: laravel vuejs2

我有一个Laravel 5.5应用程序(不使用Passport),我正在尝试从使用stock基础结构构建的VueJS组件访问do.call( cbind, c(list(DT.list[[1]][,.(x,y)]), lapply(DT.list, `[`,,!c("x","y"))) ) # way too many brackets...but I think it works ## x y v1 v2 v3 v4 v5 v6 ## 1: 1 a 1 3 5 7 9 11 ## 2: 1 a 2 4 6 8 10 12 路由。使用vue-resource我无法获得有效的,经过身份验证的响应。我可以在这里看到传递的XSRF令牌:

enter image description here

我的VueJS:

/api

我在 methods: { updateSubscription(newChannelId, oldChannelId) { if (oldChannelId < 1 && newChannelId > 0) { console.log('new subscription', this); this.$http.post('/api/subscribe', { 'game_id': this.gameId, 'channel_id': newChannelId }).then(response => { // success }, response => { console.error('Failed to subscribe'); }); 的Laravel路线:

routes/api.php

我认为Laravel处理了XSRF,我不会遇到这个问题。我想我对Route::middleware('auth:api')->group(function () { Route::post('subscribe', 'SubscriptionController@create'); Route::post('unsubscribe', 'SubscriptionController@delete'); Route::post('update-subscription', 'SubscriptionController@update'); }); 并不是100%熟悉我的http内核看起来像:

auth:api

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我不认为你的401错误与CSRF令牌有关。您已将auth:api中间件添加到路由中,因此需要为每个请求传递有效的API令牌以验证请求。我看不出你是在做这件事。

查看TokenGuard课程,了解其内部运作方式:https://github.com/laravel/framework/blob/5.5/src/Illuminate/Auth/TokenGuard.php

基本上,您需要为每个请求传递一个名为api_token的请求参数或一个授权标头Authorization: Bearer <api_token>