Search code examples
meteorverificationaccounts

How to validate data in onCreateUser without losing the form data?


I'm writing an Appliction using Meteor. In this App I want to implement a server-side validation of the user data using Accounts.onCreateUser. There is some data passed which can only be verified on the server side.

At client side I call:

Template.register.events({
    'submit form': function (e) {
        e.preventDefault();
        var attributes = {
            username: $("#inputUsername").val(),
            password: $("#inputPassword").val(),
            confirmation: $("inputConfirmation").val(),
            email: $("#inputEmail").val(),
            ...
        };

        Accounts.createUser(attributes, function(err){
            if (err) {
                throwError(err);
            } else {
            }
        });
    }
});

And on the server side:

Accounts.onCreateUser(function(options, user) {
    if(!verifyData(options))
        throw new Meteor.Error(403, "Wrong input");
    return user;
});

After the server side verification fails, all form data is lost. What is the best way to keep the data?


Solution

  • I went ahead and reproduced your code on a Meteorpad and from what I can tell, the form data does still persist. You just need to access it via the attributes variable in the client-side.

    There may be something I am missing, but i took what you posted above and put it in there.