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.
: only open to people who have been invited The event is by invitation only.
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.
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.
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:
UserInvitations
to contain the invites to become a user.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With