node.js - javascript/socket.io -- Socket.IO authorization with twitter issue -


So I wrote a socket. IO app with 'authorization' argument to ensure that user before Twitter Before joining the user to connect to the webcuter server but the issue I am coming to is that the client.js tries to an IO.connect user before clicking on the TWITTER login (which is the authorization Check), which is a failure because it Unless it is authenticated. When a user eventually clicks on Twitter login and goes through the OAuth process, the socket does not connect because the socket authentication argument has already failed from the initial connection.

server.js

  // Route app. ('/ Author / twitter', Passport. Certify ('Twitter'), Function (Rick, Ridge) {}); Authentication ('twitter', {failRedirect: '/ login'}), function (rick, ridge) {res.redirect ('/ authed';}) ; // Socket at parskin = essential ('connect'). Utils.parseCookie; Io.configure (function () {io.set ('authorization', function (data, accept) {if (data.headers.cookie) {data.cookie = parseCookie (data.headers.cookie); data.sessionID = data .cookie ['session.sid']; sessionStore.get (data sessionID, function (error, session) {if (err!! Session || (typeof session.twitter.user == 'undefined')) {accept ( Accept (empty, false);} and (data session = session; accept (empty, true);}}})}} {accept refund (empty, wrong);}}); io.sockets.on ('connection' , Function (socket) {console.log ('authenticated!'); Socket.on ('hooray', function () {console.log ('receive client message');})}}});  < / Pre> 

client.js

  Var socket = io.connect (http://example.com); // server.js Connect to the code socket. ('Connect', Tasks) {To confirm the user (server) for Twitter link url Warning ('Client Connect!'); SocketMet ('Hooray');});  Twitter Login! & Lt; / a & Gt;   

So there is a problem ... No 'connection' on the server side nor the 'connect' function on the client side is run because the user has not logged on to Twitter yet When the These logs Yogkrta end, the socket authentication function has already failed.

How can I create it so that the customer does not actually try to connect before logging in? Can I save it in another file and import it later? Can I write some server callbacks in the authorization function? My JavaScript is weak because I'm learning JavaScript with socket. Please advise, many thanks.

If you want to use this, the code will look something like this: < P> server.js

  app.get ("/ authed", function (req, res) {res.render ("authed", {});});   

layout.ess

  & lt; Body & gt; & Lt; Div & gt; & Lt;% - body% & gt; & Lt; / Div & gt; & Lt; / Body & gt;   

index.ejs

  & lt; A href = "www.example.com/twitter/auth" & gt; Twitter Login! & Lt; / A & gt;   

authed.ejs

  & lt; Script type = "text / javascript" src = "/ socket.io/socket.io.js"> & Lt; / Script & gt; & Lt; Script type = "text / javascript" & gt; Var socket = io.connect (); Socket.one ('connect', function () {warning ('client connect!'); Socket.mate ('hooray');}); & Lt; / Script & gt; <...        

Comments