gotta have my pops
gotta have my pops

Reputation: 898

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

So I wrote a Socket.IO app with 'authorization' logic to make sure user is logged in to Twitter before it allows the user to connect to the websocket server. But the issue I'm having is that the client.js tries to do an io.connect BEFORE the user clicks on TWITTER LOGIN (that io authorization checks), which FAILS because it's not yet authenticated. When the user finally clicks on Twitter Login and goes through the OAuth process, the socket doesn't connect since the socket authentication logic already failed from the initial connection.

server.js

// routes
app.get('/auth/twitter', passport.authenticate('twitter'), function(req, res){});
app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/login' }), function(req, res){
    res.redirect('/authed');
});

// socket auth
parseCookie = require('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 (err, session) {
            if (err || !session || (typeof session.twitter.user == 'undefined')) {
                accept(null, false);
            }
            else {
                data.session = session;
                accept(null, true);
            }
        });
    }
    else {
        return accept(null, false);
    }
});

io.sockets.on('connection', function (socket) {
    console.log('authenticated!');
    socket.on('hooray', function(){
        console.log('client message received');
    });
});

client.js

var socket = io.connect(http://example.com); // connect to server.js code
socket.on('connect', function() {
    alert('client connected!');
    socket.emit('hooray');
});

// code for twitter link url for user to authenticate
<a href="www.example.com/twitter/auth">twitter login!</a>

So the issue is.. neither the 'connection' on server side nor the 'connect' function on client side runs because the user never logged in to twitter yet. When the user finally logs in, the socket authorization function already failed.

How can I make it so the client doesn't try to connect before the user actually logs in? Do I save it in a different file and import it later on? Do I write some server callback in the authorization function? My javascript is weak as I am learning javascript along with socket.io. Please advise, many thanks.

Upvotes: 1

Views: 1259

Answers (1)

patalmypal
patalmypal

Reputation: 6712

If you're using ejs to render, the code would look something like this:

server.js

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

layout.ejs

  <body>
    <div>
      <%- body %>
    </div>
  </body>

index.ejs

  <a href="www.example.com/twitter/auth">twitter login!</a>

authed.ejs

  <script type="text/javascript" src="/socket.io/socket.io.js"></script>
  <script type="text/javascript">
  var socket = io.connect();
  socket.on('connect', function() {
     alert('client connected!');
     socket.emit('hooray');
  });
  </script>

  ...
  ...

  render whatever else

Hope this helps.

Upvotes: 2

Related Questions