Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 would I do that in meteor?

In general, the way I see it, I can do it when the user is created for the first time (ideal), or check to see whether they need a new todo every time they log in (less ideal). In the latter case, I can do a check for Todos.findOne(), and if the count is 0, add one. However, seems that whether I do this in my router when the page loads, or on some template's .rendered function, the collection I'm checking hasn't been loaded yet, so I always create a new todo, even if one really does exist. So it'd be great if someone could explain how to get around that.

But, what I'd ideally want is the ability to just create a new Todo when the user is created. There is a Accounts.onCreateUser method, but that is used to add additional info to user profile, not a post-create hook. There's also a method to programmatically create the user using Accounts.createNewUser with a callback, but I'm using the accounts-ui package so am not programmatically adding users. In a less ideal case, I could check for the Todo whenever the user logs in, but even in that case, there seems to be a federated Accounts.loginWithXService login, so not sure how to handle the callback when any user logs in, regardless of service type.

I think I must be missing something simple, so apologies if this is super obvious. Any help is appreciated.

like image 907
jeffthink Avatar asked Oct 20 '12 02:10

jeffthink


People also ask

Which of the following user accounts packages are provided by the meteor developer group?

Here's a complete list of login providers for which Meteor actively maintains core packages: Facebook with accounts-facebook. Google with accounts-google. GitHub with accounts-github.

What is the meteor User () function for?

The Meteor Accounts system builds on top of the userId support in publish and methods . The core packages add the concept of user documents stored in the database, and additional packages add secure password authentication, integration with third party login services, and a pre-built user interface.


1 Answers

The Meteor API now has the hook onCreateUser:

Accounts.onCreateUser(function (options, user) {   Todos.insert({     owner: user._id,     text: "First todo to cross off!",   });    // We still want the default hook's 'profile' behavior.   if (options.profile)     user.profile = options.profile;    return user; }); 
like image 64
David Braun Avatar answered Sep 28 '22 05:09

David Braun