SocketCluster Middleware HandShake with promise

梦想与她 提交于 2019-12-06 11:35:40

A big reason why JWT is used on SocketCluster is to handle logging in and authentication, have you considered just using WS?

Take a look at SocketCluster authentication.


Just how your current HTTP code check the login data, you can do the same for WS and use socket.setAuthToken to set the token (here is an example that I used in my project):

socket.setAuthToken({
    email: credentials.email,
    id: returnedData.id,
    permission: returnedData.permission
});

You can then do requests to the WS server still using on/emit, and do a check to see if they are authenticated. Here's a modified snippet of my authCheck function:

const token = socket.getAuthToken();

if (token && token.email) {
    console.log('Token Valid, User is: ', token.email);
    // user validated - continue with your code
} else {
    console.log('Token Invalid, User not authenticated.');
    // respond with error message to the user
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!