仅如何进行注册邀请? [英] How to make sign-up invitation only?
问题描述
使用Meteor帐户(和accounts-ui
)是否有一种简单的方法来仅制作新用户注册邀请?例如,通过提供邀请链接或邀请代码.
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.
我在Meteor文档中唯一能找到的相关信息是 Meteor.sendEnrollmentEmail ,但这不能解决我的问题.
The only thing related I could find in the Meteor documentation is Meteor.sendEnrollmentEmail but it doesn't solve my problem.
推荐答案
您可以使用内置包来做到这一点,但是我发现滚动一个简单的实现很容易且功能强大.
You can do this with the built in package, but I found it alot easier and powerful to roll a simple implementation.
您需要:
- 创建一个集合,例如
UserInvitations
以包含成为用户的邀请. - 创建用于制作
UserInvitations
的用户界面/使用meteor mongo
插入一些内容
-
使用
iron-router
或类似方法创建一条路线,例如:
- Create a collection, eg
UserInvitations
to contain the invites to become a user. - Create UI for making
UserInvitations
/ insert some usingmeteor 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
提交userRegistration
中的表单后,请致电
When the form in userRegistration
is submitted - call
Accounts.createUser({invitationId: Template.instance().data.invitationId /*,.. other fields */})
在服务器上,创建一个Accounts.onCreateUser
钩子,以将invitationId
从选项传递给用户
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;
});
此外,在服务器上创建一个Accounts.validateNewUser
钩子以检查invitationId
并将邀请标记为已使用
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
});
现在,只有拥有有效未使用的invitationId
的用户才能注册.
Now, only users that have a valid unused invitationId
can register.
2014年10月-更新为使用流星0.9.x API
Oct 2014 - Updated to use meteor 0.9.x API's
这篇关于仅如何进行注册邀请?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!