mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Wrap all of accounts-base/localstorage_token.js in a closure.
This way we don't modify window.userId.
This commit is contained in:
committed by
Nick Martin
parent
76b7a3a054
commit
3fda3bd9ca
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user