From 8442e458075ea69dd5b3ad97df4b3a1ccdab68d4 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Mon, 5 May 2014 14:56:11 +0200 Subject: [PATCH] Cleanup of session register code. --- static/js/directives/settings.js | 26 ++++++++++++++++---------- static/js/services/mediastream.js | 14 ++++++++------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/static/js/directives/settings.js b/static/js/directives/settings.js index a7c9869e..3b51c132 100644 --- a/static/js/directives/settings.js +++ b/static/js/directives/settings.js @@ -130,22 +130,28 @@ define(['underscore', 'text!partials/settings.html'], function(_, template) { $scope.registerUserid = function(btn) { + var successHandler = function(data) { + console.info("Created new userid:", data.userid); + // If the server provided us a nonce, we can do everthing on our own. + mediaStream.users.store(data); + $scope.loadedUserlogin = true; + safeApply($scope); + // Directly authenticate ourselves with the provided nonce. + mediaStream.api.requestAuthentication(data.userid, data.nonce); + delete data.nonce; + }; + console.log("No userid - creating one ..."); mediaStream.users.register(btn.form, function(data) { - console.info("Created new userid:", data.userid); if (data.nonce) { - // If the server provided us a nonce, we can do everthing on our own. - mediaStream.users.store(data); - $scope.loadedUserlogin = true; - safeApply($scope); - // Directly authenticate ourselves with the provided nonce. - mediaStream.api.requestAuthentication(data.userid, data.nonce); - delete data.nonce; + successHandler(data); } else { // No nonce received. So this means something we cannot do on our own. - // Make are GET request and retrieve nonce that way and let the + // Make are GET request and retrieve nonce that way and let the // browser/server do the rest. - // TODO(longsleep): Implement me. + mediaStream.users.authorize(data, successHandler, function(data, status) { + console.error("Failed to get nonce after create", status, data); + }); } }, function(data, status) { console.error("Failed to create userid", status, data); diff --git a/static/js/services/mediastream.js b/static/js/services/mediastream.js index 880e037d..76643cb4 100644 --- a/static/js/services/mediastream.js +++ b/static/js/services/mediastream.js @@ -86,16 +86,18 @@ define([ }, 0); var retries = 0; var authorize = function() { - mediaStream.users.authorize({}, function(data) { - console.info("Retrieved nonce - authenticating as user:", data.userid); - mediaStream.api.requestAuthentication(data.userid, data.nonce); - delete data.nonce; - }, function(data, status) { + mediaStream.users.authorize({ + count: retries + }, success_cb, function(data, status) { + // Error handler retry. retries++; if (retries <= 10) { $timeout(authorize, 2000); } else { console.error("Failed to authorize session", status, data); + if (error_cb) { + error_cb(data, status) + } } }); }; @@ -184,7 +186,7 @@ define([ return null; }, forget: function() { - localStorage.removeItem("mediastream-login"); + localStorage.removeItem("mediastream-login-"+context.Cfg.UsersMode); } }, initialize: function($rootScope, translation) {