Skip to content

Commit

Permalink
Merge pull request meteor#9512 from hwillson/issue-5127
Browse files Browse the repository at this point in the history
Pass login type to client side Accounts.onLogin callbacks
  • Loading branch information
abernix authored Jan 10, 2018
2 parents 343a62c + 04a1eff commit f551a56
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
7 changes: 7 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@
supported/maintained.
[PR #9445](https://github.com/meteor/meteor/pull/9445)

* Client side `Accounts.onLogin` callbacks now receive a login details
object, with the attempted login type (e.g. `{ type: password }` after a
successful password based login, `{ type: resume }` after a DDP reconnect,
etc.).
[Issue #5127](https://github.com/meteor/meteor/issues/5127)
[PR #9512](https://github.com/meteor/meteor/pull/9512)

## v1.6.0.1, 2017-12-08

* Node has been upgraded to version
Expand Down
20 changes: 12 additions & 8 deletions packages/accounts-base/accounts_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ Ap.callLoginMethod = function (options) {
});

// Prepare callbacks: user provided and onLogin/onLoginFailure hooks.
var loginCallbacks = _.once(function (error) {
var loginCallbacks = _.once(function ({ error, loginDetails }) {
if (!error) {
self._onLoginHook.each(function (callback) {
callback();
callback(loginDetails);
return true;
});
} else {
Expand All @@ -256,7 +256,7 @@ Ap.callLoginMethod = function (options) {
return true;
});
}
options.userCallback.apply(this, arguments);
options.userCallback(error, loginDetails);
});

var reconnected = false;
Expand Down Expand Up @@ -312,7 +312,7 @@ Ap.callLoginMethod = function (options) {
// intermediate state before the login method finishes. So we don't
// need to show a logging-in animation.
_suppressLoggingIn: true,
userCallback: function (error) {
userCallback: function (error, loginDetails) {
var storedTokenNow = self._storedLoginToken();
if (error) {
// If we had a login error AND the current stored token is the
Expand All @@ -339,7 +339,7 @@ Ap.callLoginMethod = function (options) {
// Possibly a weird callback to call, but better than nothing if
// there is a reconnect between "login result received" and "data
// ready".
loginCallbacks(error);
loginCallbacks({ error, loginDetails });
}});
}
});
Expand All @@ -365,19 +365,23 @@ Ap.callLoginMethod = function (options) {
if (error || !result) {
error = error || new Error(
"No result from call to " + options.methodName);
loginCallbacks(error);
loginCallbacks({ error });
return;
}
try {
options.validateResult(result);
} catch (e) {
loginCallbacks(e);
loginCallbacks({ error: e });
return;
}

// Make the client logged in. (The user data should already be loaded!)
self.makeClientLoggedIn(result.id, result.token, result.tokenExpires);
loginCallbacks();
loginCallbacks({
loginDetails: {
type: result.type,
},
});
};

if (!options._suppressLoggingIn)
Expand Down
29 changes: 29 additions & 0 deletions packages/accounts-base/accounts_client_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,32 @@ Tinytest.addAsync(
});
}
);

Tinytest.addAsync(
'accounts - onLogin callback receives { type: "password" } param on login',
(test, done) => {
const onLogin = Accounts.onLogin((loginDetails) => {
test.equal('password', loginDetails.type);
onLogin.stop();
removeTestUser(done);
});
logoutAndCreateUser(test, done, () => {});
}
);

Tinytest.addAsync(
'accounts - onLogin callback receives { type: "resume" } param on ' +
'reconnect, if already logged in',
(test, done) => {
logoutAndCreateUser(test, done, () => {
const onLogin = Accounts.onLogin((loginDetails) => {
test.equal('resume', loginDetails.type);
onLogin.stop();
removeTestUser(done);
});

Meteor.disconnect();
Meteor.reconnect();
});
}
);
6 changes: 6 additions & 0 deletions packages/accounts-base/accounts_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ export class AccountsCommon {
* @summary Register a callback to be called after a login attempt succeeds.
* @locus Anywhere
* @param {Function} func The callback to be called when login is successful.
* The callback receives a single object that
* holds login details. This object contains the login
* result type (password, resume, etc.) on both the
* client and server. `onLogin` callbacks registered
* on the server also receive extra data, such
* as user details, connection information, etc.
*/
onLogin(func) {
return this._onLoginHook.register(func);
Expand Down
1 change: 1 addition & 0 deletions packages/accounts-base/accounts_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ Ap._attemptLogin = function (
),
result.options || {}
);
ret.type = attempt.type;
this._successfulLogin(methodInvocation.connection, attempt);
return ret;
}
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.1"
version: "1.4.2"
});

Package.onUse(function (api) {
Expand Down

0 comments on commit f551a56

Please sign in to comment.