Skip to content

Commit

Permalink
Merge pull request #10543 from brucejo75/accounts_call_onLogin_callba…
Browse files Browse the repository at this point in the history
…cks_startup

[fix #10157] Make sure onLogin callbacks are called during startup.
  • Loading branch information
Ben Newman committed Nov 6, 2019
2 parents 00564ac + ec0ed4d commit 8da7f3b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
17 changes: 16 additions & 1 deletion packages/accounts-base/accounts_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,22 @@ export class AccountsClient extends AccountsCommon {
this._pageLoadLoginCallbacks.forEach(callback => callback(attemptInfo));
this._pageLoadLoginCallbacks = [];
this._pageLoadLoginAttemptInfo = attemptInfo;
};
}

// _startupCallback executes on onLogin callbacks
// at registration time if already logged in
// this can happen when new AccountsClient is created
// before callbacks are registered see #10157
_startupCallback(callback) {
// Are we already logged in?
if (this.connection._userId) {
// If already logged in before handler is registered, it's safe to
// assume type is a 'resume', so we execute the callback at the end
// of the queue so that Meteor.startup can complete before any
// embedded onLogin callbacks would execute.
Meteor.setTimeout(() => callback({ type: 'resume' }), 0);
}
}

///
/// LOGIN TOKENS
Expand Down
8 changes: 7 additions & 1 deletion packages/accounts-base/accounts_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ export class AccountsCommon {
* as user details, connection information, etc.
*/
onLogin(func) {
return this._onLoginHook.register(func);
let ret = this._onLoginHook.register(func);
// call the just registered callback if already logged in
this._startupCallback(ret.callback);
return ret;
}

/**
Expand Down Expand Up @@ -285,6 +288,9 @@ export class AccountsCommon {
}
return new Date() > (new Date(when) - minLifetimeMs);
}

// No-op on the server, overridden on the client.
_startupCallback(callback) {}
}

// Note that Accounts is defined separately in accounts_client.js and
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts-base/package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: "A user account system",
version: "1.4.4",
version: "1.4.5",
});

Package.onUse(api => {
Expand Down
1 change: 1 addition & 0 deletions packages/callback-hook/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class Hook {
this.callbacks[id] = callback;

return {
callback,
stop: () => {
delete this.callbacks[id];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/callback-hook/package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
summary: "Register callbacks on a hook",
version: '1.1.0'
version: '1.2.0'
});

Package.onUse(function (api) {
Expand Down

0 comments on commit 8da7f3b

Please sign in to comment.