AccountsOn Create User

On occasion, your product may need to add some extra information to a user before they're inserted into the Meteor.users collection. To help with this, Meteor includes the Accounts.onCreateUser() hook which allows us to modify the new user document just before it's inserted into Meteor.users.

New OAuth user welcome emails

In Pup, we use the Accounts.onCreateUser() hook to send new users who sign up via an OAuth provider a welcome email. This is done via the sendWelcomeEmail() module imported from /api/Users/actions/sendWelcomeEmail.js. Learn more here.

This means that, for example, if we wanted to add an extra field to our user like onboarding, we can do this in the Accounts.onCreateUser hook. In Pup, we've defined an example of using the Accounts.onCreateUser hook in /startup/server/accounts/onCreateUser.js.

By default, Meteor runs this hook behind the scenes, assigning a profile field to the user if one is passed to the Accounts.createUser() method. In Pup, our example does the exact same thing that Meteor does, however, exposes the Accounts.onCreateUser hook so it's easily available for your modification.

This file is imported into the /startup/server/accounts/index.js file by default, but you can remove it if you wish.

import { Accounts } from 'meteor/accounts-base';
import sendWelcomeEmail from '../../../api/Users/actions/sendWelcomeEmail';
import UserSettings from '../../../api/UserSettings/UserSettings';
import isOAuthUser from '../../../api/Users/actions/isOAuthUser';

Accounts.onCreateUser((options, user) => {
  const userToCreate = user;
  if (options.profile) userToCreate.profile = options.profile;
  if (isOAuthUser({ user: userToCreate })) sendWelcomeEmail({ user: userToCreate }); // NOTE: Sent for OAuth accounts only here. Sent for password accounts after email verification (https://cleverbeagle.com/pup/v2/accounts/email-verification).

  userToCreate.roles = ['user'];

  const settings = UserSettings.find().fetch();
  userToCreate.settings = settings;

  return userToCreate;