Laravel 5.6 Passport driver not working in socket.io and gives unauthorized exception

会有一股神秘感。 提交于 2019-12-01 07:36:26

问题


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.

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:


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":""
    }


来源:https://stackoverflow.com/questions/49075974/laravel-5-6-passport-driver-not-working-in-socket-io-and-gives-unauthorized-exce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!