diff --git a/resources/static/pages/start.js b/resources/static/pages/start.js index 0fdebc81429aff2bd195f61dadf1d56619f5176f..34e4c2dec70f3cfdc4588a875a3ad1f4e09198ce 100644 --- a/resources/static/pages/start.js +++ b/resources/static/pages/start.js @@ -11,6 +11,7 @@ $(function() { */ var bid = BrowserID, + helpers = bid.Helpers, pageHelpers = bid.PageHelpers, user = bid.User, dom = bid.DOM, @@ -20,9 +21,10 @@ $(function() { path = document.location.pathname, moduleManager = bid.module, modules = bid.Modules, - CodeCheck = modules.CodeCheck, + CookieCheck = modules.CookieCheck, XHRDelay = modules.XHRDelay, - XHRDisableForm = modules.XHRDisableForm; + XHRDisableForm = modules.XHRDisableForm, + ANIMATION_TIME = 500; xhr.init({ time_until_delay: 10 * 1000 }); @@ -33,76 +35,90 @@ $(function() { $(window).bind('resize', function() { $('#vAlign').css({'height' : $(window).height() }); }).trigger('resize'); } - dom.addClass("body", "ready"); - moduleManager.register("xhr_delay", XHRDelay); moduleManager.start("xhr_delay"); moduleManager.register("xhr_disable_form", XHRDisableForm); moduleManager.start("xhr_disable_form"); - if (!path || path === "/") { - bid.index(); - } - else if (path === "/signin") { - var module = bid.signIn.create(); - module.start({}); - } - else if (path === "/signup") { - bid.signUp(); - } - else if (path === "/forgot") { - bid.forgot(); - } - else if (path === "/add_email_address") { - var module = bid.addEmailAddress.create(); - module.start({ - token: token - }); + if(path && path !== "/") { + // do a cookie check on every page except the main page. + moduleManager.register("cookie_check", CookieCheck); + moduleManager.start("cookie_check", { ready: start }); } - else if(token && path === "/verify_email_address") { - bid.verifyEmailAddress(token); + else { + // the main page makes it through without checking for cookies. + start(true); } - $("a.signOut").click(function(event) { - event.preventDefault(); - event.stopPropagation(); - - user.logoutUser(function() { - document.location = "/"; - }, pageHelpers.getFailure(bid.Errors.logout)); - }); - - var ANIMATION_TIME = 500; - network.cookiesEnabled(function(cookiesEnabled) { - if(cookiesEnabled) { - user.checkAuthentication(function(authenticated) { - if (authenticated) { - displayAuthenticated(); - } - else { - displayNonAuthenticated(); - } + function start(status) { + // If cookies are disabled, do not run any of the page specific code and + // instead just show the error message. + if(!status) return; + + dom.addClass("body", "ready"); + + if (!path || path === "/") { + bid.index(); + } + else if (path === "/signin") { + var module = bid.signIn.create(); + module.start({}); + } + else if (path === "/signup") { + bid.signUp(); + } + else if (path === "/forgot") { + bid.forgot(); + } + else if (path === "/add_email_address") { + var module = bid.addEmailAddress.create(); + module.start({ + token: token }); } + else if(token && path === "/verify_email_address") { + bid.verifyEmailAddress(token); + } else { - displayNonAuthenticated(); + // Instead of throwing a hard error here, adding a message to the console + // to let developers know something is up. + helpers.log("unknown path"); } - }); - - function displayAuthenticated() { - $(".display_always").fadeIn(ANIMATION_TIME); - dom.addClass("body", "authenticated"); - $(".display_auth").fadeIn(ANIMATION_TIME); - if ($('#emailList').length) { - bid.manageAccount(); + + user.checkAuthentication(function(authenticated) { + if (authenticated) { + displayAuthenticated(); + } + else { + displayNonAuthenticated(); + } + }); + + function displayAuthenticated() { + $(".display_always,.display_auth").fadeIn(ANIMATION_TIME); + dom.addClass("body", "authenticated"); + + if ($('#emailList').length) { + bid.manageAccount(); + } + + $("a.signOut").click(function(event) { + event.preventDefault(); + event.stopPropagation(); + + user.logoutUser(function() { + document.location = "/"; + }, pageHelpers.getFailure(bid.Errors.logout)); + }); } - } - function displayNonAuthenticated() { - $(".display_always").fadeIn(ANIMATION_TIME); - dom.addClass("body", "not_authenticated"); - $(".display_nonauth").fadeIn(ANIMATION_TIME); + function displayNonAuthenticated() { + $(".display_always").fadeIn(ANIMATION_TIME); + dom.addClass("body", "not_authenticated"); + $(".display_nonauth").fadeIn(ANIMATION_TIME); + } } + }); diff --git a/resources/static/shared/helpers.js b/resources/static/shared/helpers.js index 8b2d99794caee0aca5205e818d8d5c697a4a3570..7f536f6b61a5ad65e156c14c86f14b239a060648 100644 --- a/resources/static/shared/helpers.js +++ b/resources/static/shared/helpers.js @@ -67,6 +67,15 @@ } } + function log(msg) { + try { + window.console.log(msg); + } catch(e) { + // Catch all if console is not available or if it for some reason blows + // up. Do nothing. + } + } + extend(helpers, { /** * Extend an object with the properties of another object. Overwrites @@ -116,7 +125,14 @@ * parameter is a function. * @param {variant} [params] - parameters to pass to callback. */ - complete: complete + complete: complete, + + /** + * If the console is available, log a message to it. + * @method log + * @param {string} msg + */ + log: log }); diff --git a/resources/static/shared/user.js b/resources/static/shared/user.js index 48778cd31e4e00736dde561cfa02a0a5b584fbae..aee3065b955d292649396333666c01dfa03ae811 100644 --- a/resources/static/shared/user.js +++ b/resources/static/shared/user.js @@ -648,9 +648,7 @@ BrowserID.User = (function() { // log out of browserid network.logout(function() { setAuthenticationStatus(false); - if (onComplete) { - onComplete(); - } + complete(onComplete); }, onFailure); }, @@ -700,7 +698,8 @@ BrowserID.User = (function() { }, /** - * Check whether the current user is authenticated. + * Check whether the current user is authenticated. Calls the callback + * with false if cookies are disabled. * @method checkAuthentication * @param {function} [onComplete] - Called when check is complete with one * boolean parameter, authenticated. authenticated will be true if user is @@ -708,10 +707,15 @@ BrowserID.User = (function() { * @param {function} [onFailure] - Called on error. */ checkAuthentication: function(onComplete, onFailure) { - network.checkAuth(function(authenticated) { - setAuthenticationStatus(authenticated); - if (onComplete) { - onComplete(authenticated); + network.cookiesEnabled(function(cookiesEnabled) { + if(cookiesEnabled) { + network.checkAuth(function(authenticated) { + setAuthenticationStatus(authenticated); + complete(onComplete, authenticated); + }, onFailure); + } + else { + complete(onComplete, cookiesEnabled); } }, onFailure); }, diff --git a/resources/static/test/cases/shared/helpers.js b/resources/static/test/cases/shared/helpers.js index 7e0a171def4e844649bfa204153118211c72755e..8ebdd3f7dd92c4d01e0ad85ad3785759df892254 100644 --- a/resources/static/test/cases/shared/helpers.js +++ b/resources/static/test/cases/shared/helpers.js @@ -103,4 +103,65 @@ equal(url, "https://browserid.org?email=testuser%40testuser.com&status=complete", "correct URL with GET parameters"); }); + + test("simulate log on browser without console - no exception thrown", function() { + var err, + nativeConsole = window.console; + + // Simulate browser without window.console. + window.console = undefined; + try { + helpers.log("test message"); + } + catch(e) { + err = e; + } + + equal(typeof err, "undefined", "no exception thrown"); + + window.console = nativeConsole; + }); + + test("simulate log on browser without console.log - no exception thrown", function() { + var err, + nativeConsole = window.console; + + // Simulate browser with console, but without console.log. + window.console = {}; + try { + helpers.log("test message"); + } + catch(e) { + err = e; + } + + equal(typeof err, "undefined", "no exception thrown"); + + window.console = nativeConsole; + }); + + test("simulate log on browser with console.log - prints message", function() { + var err, + loggedMessage, + nativeConsole = window.console; + + // Simulate browser with console and console.log + window.console = { + log: function(msg) { + loggedMessage = msg; + } + }; + + try { + helpers.log("test message"); + } + catch(e) { + err = e; + } + + equal(typeof err, "undefined", "no exception thrown"); + equal(loggedMessage, "test message", "correct message logged"); + + window.console = nativeConsole; + }); }());