Laravel 5.5授权政策未被调用

时间:2017-12-04 04:45:25

标签: php laravel vue.js http-status-code-403 laravel-passport

来自Home.vue的登录电话

methods: {
  login: function (e){
   e.preventDefault();
   this.standing = true; // Disables Login Button
   axios.post('/oauth/token', {
    email: this.email, // I verify with email
    password: this.password,
    username: this.username,
    grant_type: 'password',
    client_id: integer_of_client_id,
    client_secret: 'secret_token',
  }).then({response => {
     window.axios.defaults.headers.common['Authorization'] = 'Bearer '+ response.data.access_token;
     // Make call to /api/user
// ...

好的,所以我在这里检查了我的命名空间,并且我无法弄清楚Laravel是如何错过我创建的这个政策的:

AXIOS CALL on Reply.vue(删除回复)

axios.delete('/api/barracks/reply/delete/' + this.reply.id, { id: this.reply.id });

路线/ API.PHP

Route::delete('/barracks/reply/delete/{forumReply}','ForumRepliesController@destroy');

CONTROLLER

public function destroy(ForumReply $forumReply)
{

    $this->authorize('destroy', $forumReply);

    $forumReply->delete();

    return response()->json(['Success',204]);
}

AuthServiceProvider.php

// ... Stuff
use App\ForumReply;
use App\Policies\ForumReplyPolicy;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        ForumReply::class => ForumReplyPolicy::class
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
        // Passport Stuff
    }
}

FourmReplyPolicy.php

namespace App\Policies;

use App\User;
use App\ForumReply;
use Illuminate\Auth\Access\HandlesAuthorization;

class ForumReplyPolicy
{
    use HandlesAuthorization;
    public function destroy(User $user, ForumReply $forumReply)
    {
        return true;
    }
}

响应

我得到了Laravel的403回复,但对于我的生活,我找不到原因。

2 个答案:

答案 0 :(得分:0)

对于任何访问此主题并使用web.php路由的人 - >中间件

我遇到的问题是该政策未在此行上调用:

    Route::get('/post', 'PostController@index')
        ->middleware('can:index, App\Post');

问题是索引,App之间的空格字符。删除后它的作用。

答案 1 :(得分:0)

所以经过大量的游戏后,我发现了一个临时的解决方案,不幸的是有点难看。我已将其插入每个必要控制器的开头。

$user = \JWTAuth::toUser(\JWTAuth::getToken());
\Auth::loginUsingID($user->id);

然后我可以使用授权策略并检索经过身份验证的用户信息。