如何只制作注册邀请? [英] 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
的 UI/插入一些使用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 月 - 更新为使用meteor 0.9.x API
Oct 2014 - Updated to use meteor 0.9.x API's
这篇关于如何只制作注册邀请?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!