Skip to content
This repository
Browse code

updated persona api; fixed media bug

  • Loading branch information...
commit 59ce328d256fa56c5dd68a31a06c29b0a33a64fb 1 parent 82fc4ae
Jen Fong-Adwent authored September 12, 2012
1  app.js
@@ -20,6 +20,7 @@ var io = require('socket.io').listen(app);
20 20
 io.configure(function() {
21 21
   io.set('transports', ['xhr-polling']);
22 22
   io.set('polling duration', 10);
  23
+  io.set('log level', 1);
23 24
 });
24 25
 
25 26
 io.sockets.on('connection', function(socket) {
4  lib/authenticate.js
@@ -12,12 +12,12 @@ exports.verify = function(req, nconf, callback) {
12 12
   var authUrl = nconf.get('authUrl') + '/verify';
13 13
   var siteUrl = nconf.get('domain') + ':' + nconf.get('authPort');
14 14
 
15  
-  if (!req.body.bid_assertion) {
  15
+  if (!req.body.assertion) {
16 16
     return callback(new Error('Invalid assertion'));
17 17
   }
18 18
 
19 19
   var qs = {
20  
-    assertion: req.body.bid_assertion,
  20
+    assertion: req.body.assertion,
21 21
     audience: siteUrl
22 22
   };
23 23
 
5  lib/noodle-redis.js
@@ -11,8 +11,8 @@ var PROFILE_MESSAGE_MATCH = /profile-/i;
11 11
 var ME_MATCH = /^(\/me\s)/i;
12 12
 var SLASH_MATCHER = /^(\/)\w?/i;
13 13
 var RECENT_MESSAGE_LIMIT = 19; // limit recent messages to 20, starting at 0
14  
-var RECENT_MEDIA_LIMIT = 4; // limit recent media to 5, starting at 0
15  
-var RECENT_USER_LIMIT = 19; // limit recent users to 20, starting at 0
  14
+var RECENT_MEDIA_LIMIT = 9; // limit recent media to 10, starting at 0
  15
+var RECENT_USER_LIMIT = 39; // limit recent users to 40, starting at 0
16 16
 var content = require('../lib/web-remix');
17 17
 var messageMaker = require('../lib/message-maker');
18 18
 var scaffold = require('../lib/scaffold');
@@ -77,7 +77,6 @@ exports.setRecentMessage = function(client, req, io, callback) {
77 77
             message.message.match(MEDIA_AUDIO_MATCHER) ||
78 78
             (message.message.match(MEDIA_IMAGE_MATCHER) &&
79 79
             message.message.indexOf('class="emoti"') === -1)) {
80  
-
81 80
           client.lpush('channelMedia:' + channel, jsonMessage);
82 81
           client.ltrim('channelMedia:' + channel, 0, RECENT_MEDIA_LIMIT);
83 82
         }
2  public/javascripts/lib/commands.js
@@ -49,7 +49,7 @@ define(['jquery'],
49 49
       // if this is a logout trigger, log the user out
50 50
       } else if(commandMatched(logoutMatcher)) {
51 51
         commandIsMatched = true;
52  
-        document.location.href = '/about/' + $('body').data('channel') + '/logout';
  52
+        navigator.id.logout();
53 53
 
54 54
       // switch fonts
55 55
       } else if(commandMatched(fontMatcher)) {
2  public/javascripts/lib/messages.js
@@ -7,7 +7,7 @@ define(['jquery', 'commands', 'time-format', 'version-timeout'],
7 7
   var messageList = $('body #messages ol');
8 8
   var currentChannel = $('body').data('channel');
9 9
   var logLimit = 80;
10  
-  var mediaLimit = 5;
  10
+  var mediaLimit = 10;
11 11
   var myPost = false;
12 12
   var mediaIframeMatcher = /<iframe\s.+><\/iframe>/i;
13 13
   var mediaObjectMatcher = /<object\s.+><\/object>/i;
51  public/javascripts/main.js
@@ -12,8 +12,8 @@ define(['jquery', 'commands', 'messages', 'users'],
12 12
   function($, commands, messages, users) {
13 13
 
14 14
   var currentChannel = $('body').data('channel');
15  
-  var loginForm = $('#login');
16 15
   var messageForm = $('#message');
  16
+  var login = $('#login');
17 17
   var socket = io.connect(location.protocol + '//' + location.hostname +
18 18
     (location.port ? ':' + location.port : ''));
19 19
 
@@ -28,23 +28,42 @@ define(['jquery', 'commands', 'messages', 'users'],
28 28
     users.setUserList(data);
29 29
   });
30 30
 
31  
-  $('#login').click(function() {
32  
-    navigator.id.getVerifiedEmail(function(assertion) {
33  
-      if (assertion) {
34  
-        var loginFormEl = loginForm.find('form');
  31
+  $('#login').click(function(ev) {
  32
+    ev.preventDefault();
  33
+    navigator.id.request();
  34
+  });
35 35
 
36  
-        loginFormEl.find('input:first').val(assertion);
37  
-        $.post('/about/' + $('body').data('channel') + '/login',
38  
-          loginFormEl.serialize(), function (data) {
39  
-          loginForm.removeClass('enabled').addClass('disabled');
  36
+  navigator.id.watch({
  37
+    loggedInEmail: currentUser,
  38
+    onlogin: function(assertion) {
  39
+      $.ajax({
  40
+        type: 'POST',
  41
+        url: '/login',
  42
+        data: { assertion: assertion },
  43
+        success: function(res, status, xhr) {
40 44
           messageForm.removeClass('disabled').addClass('enabled');
41  
-          messageForm.find('form').addClass('font' + data.font);
42  
-        });
43  
-      }
44  
-    });
45  
-
46  
-    return false;
47  
-  });
  45
+          messageForm.find('form').addClass('font' + res.font);
  46
+          login.remove();
  47
+          currentUser = res.email;
  48
+        },
  49
+        error: function(res, status, xhr) {
  50
+          alert('login failure ' + res);
  51
+        }
  52
+      });
  53
+    },
  54
+    onlogout: function() {
  55
+      $.ajax({
  56
+        type: 'GET',
  57
+        url: '/logout',
  58
+        success: function(res, status, xhr) {
  59
+          window.location.reload();
  60
+        },
  61
+        error: function(res, status, xhr) {
  62
+          console.log('logout failure ' + res);
  63
+        }
  64
+      });
  65
+    }
  66
+});
48 67
 
49 68
   $(window, 'input').focus(function() {
50 69
     messages.clearUnreadMessages(currentChannel);
18  routes/auth.js
@@ -6,10 +6,11 @@ var noodleRedis = require('../lib/noodle-redis');
6 6
 
7 7
 module.exports = function(client, nconf, app, io, isLoggedIn) {
8 8
   // Login
9  
-  app.post('/about/:channel/login', function(req, res) {
  9
+  app.post('/login', function(req, res) {
10 10
     auth.verify(req, nconf, function(err, email) {
11 11
       if (err || !email) {
12  
-        res.json({ 'status': 500, 'error': err });
  12
+        res.status(500);
  13
+        res.json({ 'error': err });
13 14
 
14 15
       } else {
15 16
         var channel = escape(req.params.channel);
@@ -23,17 +24,20 @@ module.exports = function(client, nconf, app, io, isLoggedIn) {
23 24
           req.session.emailHash = userHash.emailHash;
24 25
           messageMaker.getMessage(client, channel, req, io, 'joined', function(errMsg, message) {
25 26
             if (errMsg) {
26  
-              res.json({ 'status': 500, 'error': errMsg });
  27
+              res.status(500);
  28
+              res.json({ 'error': errMsg });
27 29
 
28 30
             } else {
29 31
               noodleRedis.getUserlist(client, channel, function(errUser, userList) {
30 32
                 if (errUser) {
31  
-                  res.json({ 'status': 500, 'error': errUser });
  33
+                  res.status(500);
  34
+                  res.json({ 'error': errUser });
32 35
 
33 36
                 } else {
34 37
                   io.sockets.in(channel).emit('userlist', userList);
35 38
                   io.sockets.in(channel).emit('message', message);
36 39
                   res.json({
  40
+                    'email': req.session.email,
37 41
                     'channel': channel,
38 42
                     'font': req.session.userFont
39 43
                   });
@@ -47,8 +51,10 @@ module.exports = function(client, nconf, app, io, isLoggedIn) {
47 51
   });
48 52
 
49 53
   // Logout
50  
-  app.get("/about/:channel/logout", isLoggedIn, function(req, res) {
  54
+  app.get('/logout', isLoggedIn, function(req, res) {
51 55
     req.session.reset();
52  
-    res.redirect('/');
  56
+    res.json({
  57
+      'message': 'logged out'
  58
+    });
53 59
   });
54 60
 };
27  routes/message.js
@@ -11,20 +11,29 @@ module.exports = function(client, nconf, app, io, isLoggedIn) {
11 11
     var channel = escape(req.params.channel);
12 12
     noodleRedis.getRecentMessages(client, channel, function(err, messages) {
13 13
       if (err) {
14  
-        res.json({ 'status': 500, 'error': userErr });
  14
+        res.status(500);
  15
+        res.json({ 'error': err });
15 16
 
16 17
       } else {
17  
-        var channelMessages = {};
  18
+        noodleRedis.getRecentMedia(client, channel, function(errMedia, medias) {
  19
+          if (errMedia) {
  20
+            res.status(500);
  21
+            res.json({ 'error': errMedia });
18 22
 
19  
-        channelMessages.generic = messages || {};
20  
-        channelMessages.media = {};
  23
+          } else {
  24
+            var channelMessages = {};
21 25
 
22  
-        io.sockets.in(channel).emit('userlist', []);
  26
+            channelMessages.generic = messages || {};
  27
+            channelMessages.media = medias || {};
23 28
 
24  
-        res.json({
25  
-          'messages': channelMessages,
26  
-          'connected_clients': io.sockets.clients(channel).length,
27  
-          'user_list': []
  29
+            io.sockets.in(channel).emit('userlist', []);
  30
+
  31
+            res.json({
  32
+              'messages': channelMessages,
  33
+              'connected_clients': io.sockets.clients(channel).length,
  34
+              'user_list': []
  35
+            });
  36
+          }
28 37
         });
29 38
       }
30 39
     });
4  test/test.authenticate.js
@@ -26,7 +26,7 @@ describe('login', function() {
26 26
 
27 27
       var params = {
28 28
         body: {
29  
-          bid_assertion: qs.assertion
  29
+          assertion: qs.assertion
30 30
         }
31 31
       };
32 32
 
@@ -46,7 +46,7 @@ describe('login', function() {
46 46
 
47 47
       var params = {
48 48
         body: {
49  
-          bid_assertion: qs.assertion
  49
+          assertion: qs.assertion
50 50
         }
51 51
       };
52 52
 
2  views/index.jade
@@ -6,8 +6,6 @@ mixin messageForm(enabled)
6 6
 
7 7
 mixin loginForm(enabled)
8 8
   a(href='#', id='login', class='#{enabled}') Log in
9  
-    form(method='post', action='/about/#{channel}/login', id='login-form')
10  
-      input(type='hidden', name='bid_assertion', value='1')
11 9
 
12 10
 .wrapper
13 11
   block header
6  views/layout.jade
@@ -18,6 +18,10 @@ html
18 18
     != body
19 19
 
20 20
     #footer
21  
-      script(src='https://browserid.org/include.js', type='text/javascript')
  21
+      script(type='text/javascript')
  22
+        var currentUser = null;
  23
+      if session.email
  24
+        != '<script type="text/javascript">currentUser = "' + session.email + '";</script>'
  25
+      script(src='https://login.persona.org/include.js', type='text/javascript')
22 26
       script(src='/socket.io/socket.io.js')
23 27
       script(src='/javascripts/require.js', data-main='/javascripts/main')

0 notes on commit 59ce328

Please sign in to comment.
Something went wrong with that request. Please try again.