Skip to content

Commit

Permalink
Wrap all of accounts-base/localstorage_token.js in a closure.
Browse files Browse the repository at this point in the history
This way we don't modify window.userId.
  • Loading branch information
avital authored and n1mmy committed Feb 12, 2013
1 parent 76b7a3a commit 3fda3bd
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions packages/accounts-base/localstorage_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
};
// 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();
}
});
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;
// 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();
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;
};
// != 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();
};
// 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);
})();

setInterval(Accounts._pollStoredLoginToken, 3000);

0 comments on commit 3fda3bd

Please sign in to comment.