Search code examples
phplaravelvue.jshttp-status-code-403laravel-passport

Laravel 5.5 Authorization Policy Not Being Called


Login Call from 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
// ...

Alrighty so I've double and triple checked my namespacing here and I'm not able to figure out how Laravel is missing this policy I've created:

AXIOS CALL on Reply.vue (to delete reply)

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

ROUTES/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;
    }
}

Response

I'm getting a 403 response from Laravel and for the life of me I cannot find why.


Solution

  • So after a lot of playing around this I found a temporary workaround that's unfortunately a little ugly. I've inserted this into the beginning of every controller necessary.

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

    Then I can use the authorization policy and retrieve authenticated User Info.