Tal Gvili
Tal Gvili

Reputation: 319

Sails, Sockets, and Authorization

I want to build a chat like whatsapp. (Each user has a contact list which he can speak only with them) My server auth is based on facebook tokens. And I would like my sockets auth to be the same.

The current scenario : -- after the user logged in, the front end sends a socket connection request to my sails server. -- In the server "beforeConnect" function, I find the user by his fb token. -- the user is found , the socket is connected successfully.

The problem : I need the user details to be available on the socket, That's because I want to find his chats, and to update him about only his chats and new messages.

That's how my beforeConnect function looks like now. enter image description here

P.S: Don't know if it's worth mentioning, but I'm using socket.io in the client side, and not the sails.io.js I saw in the documents to connect my sails Server sockets.

Upvotes: 0

Views: 1085

Answers (1)

Arjun Kava
Arjun Kava

Reputation: 6151

onConnect method is deprecated for newer version, you can do authorization with token on connect event , you have to store token while connect and check on other listener for authentication, put this code in config/bootstrap.js

module.exports.bootstrap = function(cb) { 

    // handle connect socket first event executes after logged in
    sails.io.on('connect', function (socket){
        // store facebook token here
    });

    // handle custom listener for other stuff
    sails.io.on('doSomeStuff', function (socket){
        // check facebook token match with requested token
    });
cb();
};

client : you can simple emit "doSomeStuff" after logged in with facebook and pass token with each request

Upvotes: 2

Related Questions