Reputation: 644
I've added a form on the front page of my meteor app taking an email and name of users. It sends an enrollment email with a link for them to create a password. There are a lot of background profile settings that need to be added when the account is created and it functions from the {{loginButtons}} account creation. For some reason, onCreateUser isn't functioning when called from createUser. Where have I gone wrong? Thanks in advance.
Registration form:
<form role="form" class="form-horiztonal" id="signup">
<div class="form-group">
<label for="name" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input name="name" id="name" class="form-control" type="text" placeholder="Name"/>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
<input name="email" class="form-control" type="email" placeholder="[email protected]"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3">
<input type="submit" value="Get Started!" class="btn btn-success"/>
</div>
</div>
</form>
Page Event:
Template.landing.events({
'submit form#signup' : function(e) {
e.preventDefault();
var user = {
name: $(e.target).find('[name=name]').val(),
email: $(e.target).find('[name=email]').val()
}
Meteor.call('signup', user, function(error, id) {
if (error) {
// display the error to the user
console.log(error);
} else {
Router.go('awesome');
}
});
}
});
Signup Method (in server):
Meteor.methods({
signup: function(user) {
// ensure the user is logged in
if (!user)
throw new Meteor.Error(401, "Can't make a user without a user object");
// ensure the user has a name
if (!user.name)
throw new Meteor.Error(422, 'Please fill in a name');
// ensure there is an email
if (!user.email)
throw new Meteor.Error(424, 'Please fill in an email address');
var userId = Accounts.createUser(user);
if(userId) {
Accounts.sendEnrollmentEmail(userId);
return userId;
}
}
});
onCreateUser (in server):
Accounts.onCreateUser(function(options, user) {
if (options.profile) {
user.profile = options.profile; //Copy initial profile settings
//Set roles
user.roles = ["User"];
//Account settings
user.profile.active = false; //Account is not active on creation
user.profile.trial = true; //Accounts have 1 month trial
user.profile.expiration = moment().add('M', 1); //No expiration date on unactivated accounts
user.profile.bill = null; //Bill monthly, yearly
user.profile.ppId = null; //Paypal Id for associated credit card
user.profile.resources = 0; //Number of resources an account has created
user.profile.resourceLimit = 2; //Limit for the number of resources an account can have
//User settings
user.profile.name = null; //Name is blank when created
user.profile.phone = null; //Phone is blank when created
user.profile.createdOn = moment().toISOString(); //Createdon tracks when account created
}
return user;
});
Upvotes: 1
Views: 1674
Reputation: 7680
You aren't providing a profile
field when you call Accounts.createUser(options)
. So when your onCreateUser()
function tests for:
if (options.profile) {
// ...
}
options.profile
doesn't exist and none of your custom fields are added. Try changing your code to something like this:
Accounts.onCreateUser(function(options, user) {
// Use provided profile in options, or create an empty profile object
user.profile = options.profile || {};
user.roles = ["User"];
// Add additional fields
return user;
});
Also note that the profile
is intended for user-modifiable fields by convention and is published to the user by default. So most of those fields you're adding to the profile
should probably be added to the user object directly or to a separate user field of your choice. From the Meteor docs:
profile
: an Object which (by default) the user can create and update with any data. ... By default, the current user'susername
,emails
andprofile
are published to the client.
Upvotes: 6