From 3fda3bd9caf6d9ee3e1e40a6bc1785dee6dfec8e Mon Sep 17 00:00:00 2001 From: Avital Oliver Date: Tue, 12 Feb 2013 11:50:33 -0800 Subject: [PATCH] Wrap all of accounts-base/localstorage_token.js in a closure. This way we don't modify window.userId. --- packages/accounts-base/localstorage_token.js | 107 ++++++++++--------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/packages/accounts-base/localstorage_token.js b/packages/accounts-base/localstorage_token.js index be05e319e0..9ee0a61fda 100644 --- a/packages/accounts-base/localstorage_token.js +++ b/packages/accounts-base/localstorage_token.js @@ -36,58 +36,59 @@ Accounts._storedUserId = function() { return localStorage.getItem(userIdKey); }; + + // Login with a Meteor access token + // + Meteor.loginWithToken = function (token, callback) { + Accounts.callLoginMethod({ + methodArguments: [{resume: token}], + userCallback: callback}); + }; + + if (!Accounts._preventAutoLogin) { + // Immediately try to log in via local storage, so that any DDP + // messages are sent after we have established our user account + var token = Accounts._storedLoginToken(); + if (token) { + // On startup, optimistically present us as logged in while the + // request is in flight. This reduces page flicker on startup. + var userId = Accounts._storedUserId(); + userId && Meteor.default_connection.setUserId(userId); + Meteor.loginWithToken(token, function (err) { + if (err) { + Meteor._debug("Error logging in with token: " + err); + Accounts._makeClientLoggedOut(); + } + }); + } + } + + // Poll local storage every 3 seconds to login if someone logged in in + // another tab + Accounts._lastLoginTokenWhenPolled = token; + Accounts._pollStoredLoginToken = function() { + if (Accounts._preventAutoLogin) + return; + + var currentLoginToken = Accounts._storedLoginToken(); + + // != instead of !== just to make sure undefined and null are treated the same + if (Accounts._lastLoginTokenWhenPolled != currentLoginToken) { + if (currentLoginToken) + Meteor.loginWithToken(currentLoginToken); // XXX should we pass a callback here? + else + Meteor.logout(); + } + Accounts._lastLoginTokenWhenPolled = currentLoginToken; + }; + + // Semi-internal API. Call this function to re-enable auto login after + // if it was disabled at startup. + Accounts._enableAutoLogin = function () { + Accounts._preventAutoLogin = false; + Accounts._pollStoredLoginToken(); + }; + + setInterval(Accounts._pollStoredLoginToken, 3000); })(); -// Login with a Meteor access token -// -Meteor.loginWithToken = function (token, callback) { - Accounts.callLoginMethod({ - methodArguments: [{resume: token}], - userCallback: callback}); -}; - -if (!Accounts._preventAutoLogin) { - // Immediately try to log in via local storage, so that any DDP - // messages are sent after we have established our user account - var token = Accounts._storedLoginToken(); - if (token) { - // On startup, optimistically present us as logged in while the - // request is in flight. This reduces page flicker on startup. - var userId = Accounts._storedUserId(); - userId && Meteor.default_connection.setUserId(userId); - Meteor.loginWithToken(token, function (err) { - if (err) { - Meteor._debug("Error logging in with token: " + err); - Accounts._makeClientLoggedOut(); - } - }); - } -} - -// Poll local storage every 3 seconds to login if someone logged in in -// another tab -Accounts._lastLoginTokenWhenPolled = token; -Accounts._pollStoredLoginToken = function() { - if (Accounts._preventAutoLogin) - return; - - var currentLoginToken = Accounts._storedLoginToken(); - - // != instead of !== just to make sure undefined and null are treated the same - if (Accounts._lastLoginTokenWhenPolled != currentLoginToken) { - if (currentLoginToken) - Meteor.loginWithToken(currentLoginToken); // XXX should we pass a callback here? - else - Meteor.logout(); - } - Accounts._lastLoginTokenWhenPolled = currentLoginToken; -}; - -// Semi-internal API. Call this function to re-enable auto login after -// if it was disabled at startup. -Accounts._enableAutoLogin = function () { - Accounts._preventAutoLogin = false; - Accounts._pollStoredLoginToken(); -}; - -setInterval(Accounts._pollStoredLoginToken, 3000);