Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sign-up invitation only?

Using Meteor accounts (and accounts-ui) is there an easy way to make new user sign-ups invitation only? For example by providing an invitation link or an invitation code.

The only thing related I could find in the Meteor documentation is Meteor.sendEnrollmentEmail but it doesn't solve my problem.

like image 237
Tobias Avatar asked Jan 08 '14 08:01

Tobias


People also ask

What means Invite-only?

: only open to people who have been invited The event is by invitation only.

How do invite-only apps work?

Invite-Only Apps are applications that require you to use the apps only after being invited to them by the company or current user. The development idea is simple where the company does a test run and collects feedback on user experience. They may have a waitlist or might have planned for a certain target group.

How do I invite someone on Marketplace?

To give new users the invitation code, you have to send them an invitation email. You can find the invitation email option in your top bar named “Invite new members”. The email you send will automatically include the invitation code if you have set your marketplace to invite-only.


1 Answers

You can do this with the built in package, but I found it alot easier and powerful to roll a simple implementation.

You'll need to:

  • Create a collection, eg UserInvitations to contain the invites to become a user.
  • Create UI for making UserInvitations / insert some using meteor mongo
  • Using iron-router or similar create a route, eg:

    Router.map ->
      @route 'register',
        path: '/register/:invitationId'
        template: 'userRegistration'
        data: ->
          return {
            invitationId: @params.invitationId
          }
        onBeforeAction: ->
          if Meteor.userId()?
            Router.go('home')
          return
    
  • When the form in userRegistration is submitted - call

    Accounts.createUser({invitationId: Template.instance().data.invitationId /*,.. other fields */})
    
  • On the server, make an Accounts.onCreateUser hook to pass through the invitationId from options to the user

    Accounts.onCreateUser(function(options, user){
      user.invitationId = options.invitationId
      return user;
    });
    
  • Also, on the server make an Accounts.validateNewUser hook to check the invitationId and mark the invitation as used

    Accounts.validateNewUser(function(user){
      check(user.invitationId, String);
      // validate invitation
      invitation = UserInvitations.findOne({_id: user.invitationId, used: false});
      if (!invitation){
        throw new Meteor.Error(403, "Please provide a valid invitation");
      }
      // prevent the token being re-used.
      UserInvitations.update({_id: user.invitationId, used: false}, {$set: {used: true}});
    
      return true
    });
    

Now, only users that have a valid unused invitationId can register.

EDIT: Oct 2014 - Updated to use meteor 0.9.x API's

like image 73
nathan-m Avatar answered Sep 30 '22 19:09

nathan-m