From 3c30d30368fbc1583db5d9a0412f92370efc698c Mon Sep 17 00:00:00 2001 From: Jason Haines Date: Sat, 16 Apr 2016 17:00:05 +1000 Subject: [PATCH] 'unauthorized' event handler The example doesn't give any notification on an Unauthorised error. It would be nice to log something on the server too, but I don't see how to do this. --- .../socketsio-auth0-sample/views/index.jade | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/example/socketsio-auth0-sample/views/index.jade b/example/socketsio-auth0-sample/views/index.jade index fb90db1..3518ad8 100644 --- a/example/socketsio-auth0-sample/views/index.jade +++ b/example/socketsio-auth0-sample/views/index.jade @@ -46,8 +46,12 @@ block content function openChat() { var socket = io(); - socket.on('connect', function () { - socket.on('authenticated', function () { + socket + .on('connect', function (msg) { + console.log("connected"); + socket.emit('authenticate', {token: userToken}); // send the jwt + }) + .on('authenticated', function () { //Do $('#login').hide(); $('#chat').show(); @@ -56,10 +60,13 @@ block content $('#m').val(''); return false; }); - socket.on('chat message', function (msg) { - console.log("msg"); - $('#messages').append($('
  • ').text(msg)); - }); - }).emit('authenticate', {token: userToken}); // send the jwt - }) + }) + .on('unauthorized', function(msg){ + console.log("unauthorized: " + JSON.stringify(msg.data)); + throw new Error(msg.data.type); + }) + .on('chat message', function (msg) { + console.log("msg"); + $('#messages').append($('
  • ').text(msg)); + }); };