Laravel 5.6 Passport driver not working in socket.

2019-02-19 05:32发布

Below code was working perfectly when the driver was api. Then I created a new project and changed the driver to passport.

Now, I am always getting Error: Unauthorized. I can confirm that the request header has authorization token code in the browser. Please check the below image by clicking it and click on zoom to see the image with better quality.

enter image description here

Am I missing anything in below code? Please let me know if you need more info.

bootstrap.js

import Echo from 'laravel-echo'
window.Echo = new Echo({
   broadcaster: 'socket.io',
   host: window.location.hostname + ':6001',
   auth: {
       headers: {
           Authorization: 'Bearer' + localStorage.getItem("token")
       }
   }
});

Code to post the message using Web socket.io

var config = { 
    headers : {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + localStorage.getItem('token')
    }
};

var request = {
    "channel": "private-Send-Message-Channel." + message.Sent_To,
    "name": "MessengerEvent",
    "data": {
        "msg": message
    },
    "socket_id": socketId
};
var url = "http://localhost:6001/apps/786d1c1c820025f4/events?auth_key=be1699f0101880f673fdff7ff203334f";

axios.post(url, JSON.stringify(request), this.config).then(response => {
    //success callback
});

When the last line of code runs (axios.post) , it gives 403, Unauthorized exception.

I can confirm that the token is present in the header.

Update - 1

I have two sample projects. One with driver = api and another with driver = passport. When I am using driver : api, there is no issue. The above issue is only with driver = passport.

1条回答
冷血范
2楼-- · 2019-02-19 06:16

have you followed the passport documentation: https://laravel.com/docs/5.6/passport ?

In order to get the OAuth authentication, you need more parameters in the body apart from the bearer token.

 {
        "grant_type":"password",
        "client_id":"CLIENT_ID",
        "client_secret":"YOUR_SECRET",
        "username":"USERNAME",
        "password":"PASSWORD",
        "scope":""
    }
查看更多
登录 后发表回答