Is there a post createUser hook in meteor when using accounts-ui package?

后端 未结 6 1016
广开言路
广开言路 2020-12-03 06:58

Let\'s say I have a todo app, and I want to make sure that every user that registers has at least one todo to start with, something like \"First todo to cross off!\", how wo

6条回答
  •  渐次进展
    2020-12-03 07:52

    You can piggyback onto functions that are called by Meteor by wrapping them. I'm also using the accounts-ui and accounts-password packages and I use Underscore's _.wrap method to redefine the loginWithPassword function. Underscore is included in Meteor by default.

    I use something like this for logging in:

    Meteor.loginWithPassword = _.wrap(Meteor.loginWithPassword, function(login) {
    
      // Store the original arguments
      var args = _.toArray(arguments).slice(1),
          user = args[0],
          pass = args[1],
          origCallback = args[2];
    
      // Create a new callback function
      // Could also be defined elsewhere outside of this wrapped function
      var newCallback = function() { console.info('logged in'); }
    
      // Now call the original login function with
      // the original user, pass plus the new callback
      login(user, pass, newCallback);
    
    });
    

    In this specific case, the code above would go in your client code somewhere.

    For Accounts.createUser, it might look something like this (also somewhere in client code):

    Accounts.createUser = _.wrap(Accounts.createUser, function(createUser) {
    
      // Store the original arguments
      var args = _.toArray(arguments).slice(1),
          user = args[0],
          origCallback = args[1];
    
      // Create a new callback function
      // Could also be defined elsewhere outside of this wrapped function
      // This is called on the client
      var newCallback = function(err) {
        if (err) {
          console.error(err);
        } else {
          console.info('success');
        }
      };
    
      // Now call the original create user function with
      // the original user object plus the new callback
      createUser(user, newCallback);
    
    });
    

    Hope that's helpful.

提交回复
热议问题