How do I notify the client of success/failure when creating users with no password?

StackOverflow https://stackoverflow.com/questions/21766884

  •  11-10-2022
  •  | 
  •  

문제

I'm building an application that doesn't support public registration. New users will be created by an existing user and an enrollment email will be sent to the new user's email address. I've got this all working, but my implementation feels hackish.

Here's the code, so far.

Template:

<template name="addUser">
    <form role="form">
        <div class="form-group">
            <label for="firstName">First Name</label>
            <input name="firstName" class="form-control" placeholder="First Name" />
        </div>
        <div class="form-group">
            <label for="lastName">Last Name</label>
            <input name="lastName" class="form-control" placeholder="Last Name" />
        </div>
        <div class="form-group">
            <label for="email">Email</label>
            <input name="email" class="form-control" placeholder="Email" />
        </div>

        <button type="submit" class="btn btn-primary">{{faIcon 'plus'}} Add User</button>
    </form>
</template>

Template's Javascript:

Template.addUser.events({
    'submit form': function (e, t) {
        e.preventDefault();

        var attrs = {
            email: t.find('[name="email"]').value,
            profile: {
                firstName: t.find('[name="firstName"]').value,
                lastName: t.find('[name="lastName"]').value,
            }
        };

        Meteor.call('addUser', attrs, function (err) {
            if (err) {
                Errors.throw(err.reason);
            } else {
                Router.go('home');
            }
        });
    }
});

My addUser method

Meteor.methods({
    addUser: function (attrs) {
        var user = Meteor.user();

        if (!user) throw new Meteor.Error(401, 'Please login.');
        if (!attrs.profile.firstName) throw new Meteor.Error(422, 'Please include a first name.');
        if (!attrs.profile.lastName) throw new Meteor.Error(422, 'Please include a first name.');
        if (!attrs.email) throw new Meteor.Error(422, 'Please include an email.');

        var user = _.pick(attrs, ['firstName', 'lastName', 'email']);

        if (Meteor.isServer) {
            var newUserId = Accounts.createUser(attrs);
            Accounts.sendEnrollmentEmail(newUserId);
        }
    }
});

Since Accounts.createUser requires a password on the client side, I can't figure out how to notify the client of success or failure short of doing something hacky with Session. What's a good way to go about doing such a thing?

도움이 되었습니까?

해결책

First, put addUser method on server only (i.e. in server dir):

Meteor.methods({
  addUser: function (attrs) {
    var user = Meteor.user();

    if (!user) // you can also check this.userId here
        throw new Meteor.Error(401, 'Please login.');

    if (!attrs.profile.firstName)
       throw new Meteor.Error(422, 'Please include a first name.');

    if (!attrs.profile.lastName)
      throw new Meteor.Error(422, 'Please include a first name.');

    if (!attrs.email)
      throw new Meteor.Error(422, 'Please include an email.');

    var user = _.pick(attrs, ['firstName', 'lastName', 'email']);

    var newUserId = Accounts.createUser(attrs);
    Accounts.sendEnrollmentEmail(newUserId);
  }
});

Then, on the client, you can do something like:

 Meteor.call('addUser', attrs, function (err) {
   if (err) {
     console.log('something went wrong :(');
   }
 });

다른 팁

Give the user a default password.

(I recommend using the check() function inside your addUser method – replacing some of your if statements. Also, you don't need Meteor.isServer as long as you put the js-file inside the /server folder.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top