Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding onSuccess callback to Accounts.ui.config #479

Closed
wants to merge 1 commit into from

4 participants

@TimHeckel

I wanted to leverage the {{loginButtons}} handlebars helper method to create the account management widget, but that left me with little low-level control over the callback specified in one of the Meteor.loginWithXXX methods.

So I amended the Accounts.ui.config to accept an onSuccess callback. I'm sure my impl is not ideal, as this is my first PR for meteor. I'm happy to adjust this if need be.

What this enables me to do from the client is this:

Accounts.ui.config({
    onSuccess: function () {
        //perform addl authorization on Meteor.user() here
    }
});

Keep up the great work!

@avital
Owner

You could achieve this with the following:

Meteor.autorun(function () {
  if (Meteor.userId()) {
    // on login
  } else {
    // on logout
  }
});

Closing this, but definitely re-open it if this doesn't resolve your issue.

@avital avital closed this
@TimHeckel

Totally agree. Should have asked about that before doing the work. Thanks!

@johndevor

This works nicely, but not when I don't want it to run when the user refreshes the page. Meteor treats each manual refresh as the user logging into the site, and triggers this code each time. Is there any elegant solution to not having this run on each refresh (only on the initial login)?

In my case, I use this code to redirect the user to /home/ after they login from the root url ('/'). It fails, however, when I try to manually load the root url, as it again redirects to /home/, thinking I'm logging in again (which I suppose I technically am).

@belisarius222

Here's some code that Chris Mather and I figured out that will check whether Meteor will try to log in when the page first loads (not tested thoroughly for cross-browser compatibility):

var willTryToLogin = function() {
  return !! (window.localStorage && window.localStorage['Meteor.userId']);
};

I added this function into my routing filter chain, and now it doesn't redirect if it's trying to log in, and just hasn't finished yet.

@johndevor

That helped, thanks. Still rather ungainly, and I wish there was a true callback!

@belisarius222

true... I wrote a package called event-horizon that lets you fire events and add callbacks. There's an example of a login event in the readme.

https://github.com/belisarius222/meteor-event-horizon

@johndevor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 13, 2012
  1. @TimHeckel
This page is out of date. Refresh to see the latest.
View
4 packages/accounts-base/accounts_client.js 100644 → 100755
@@ -47,7 +47,7 @@
Accounts._makeClientLoggedOut();
throw error;
} else {
- // nothing to do
+ Accounts.ui && Accounts.ui._performOnSuccessCallback && Accounts.ui._performOnSuccessCallback();
}
});
};
@@ -65,6 +65,8 @@
data.loaded = true;
userLoadedListeners.invalidateAll();
});
+
+ Accounts.ui && Accounts.ui._performOnSuccessCallback && Accounts.ui._performOnSuccessCallback();
};
Meteor.logout = function (callback) {
View
15 packages/accounts-ui-unstyled/accounts_ui.js
@@ -10,7 +10,7 @@ if (!Accounts.ui._options) {
Accounts.ui.config = function(options) {
// validate options keys
- var VALID_KEYS = ['passwordSignupFields', 'requestPermissions'];
+ var VALID_KEYS = ['passwordSignupFields', 'requestPermissions', 'onSuccess'];
_.each(_.keys(options), function (key) {
if (!_.contains(VALID_KEYS, key))
throw new Error("Accounts.ui.config: Invalid key: " + key);
@@ -45,6 +45,19 @@ Accounts.ui.config = function(options) {
}
});
}
+
+ // wire up authentication `callback`
+ if (options.onSuccess) {
+ if (_.isFunction(options.onSuccess))
+ Accounts.ui._options.onSuccess = options.onSuccess;
+ else
+ throw new Error("Accounts.ui.config: Value for `onSuccess` must be a function");
+ }
+
+};
+
+Accounts.ui._performOnSuccessCallback = function (err) {
+ Accounts.ui._options.onSuccess && Accounts.ui._options.onSuccess();
};
Accounts.ui._passwordSignupFields = function () {
View
4 packages/accounts-ui-unstyled/accounts_ui_tests.js
@@ -13,5 +13,9 @@ Tinytest.add('accounts-ui - config validates keys', function (test) {
test.throws(function () {
Accounts.ui.config({requestPermissions: {facebook: "not an array"}});
});
+
+ test.throws(function () {
+ Accounts.ui.config({ onSuccess: "not a function" });
+ });
});
Something went wrong with that request. Please try again.