在meteor上创建用户后,会话将更改为新用户

时间:2014-06-19 23:12:16

标签: meteor meteorite

我正在创建我的自定义"创建用户"对于meteor的页面,一切都很好并且存储了用户,但是,在此之后,我的实际会话被新用户更改。即我的管理员用户是" admin"当我创建新用户" foo"时,会话变为" foo"。我在初始运行时重写了Account.createUser

这是我的代码

Template.usuarioNew.events
  'submit .form': (e) ->
    e.preventDefault()
    username = $('#username').val()
    name = $('#name').val()
    password = $('#password').val()

    roles = $('.role:checked')

    if roles.length == 0
      FlashMessages.sendWarning 'Escoge al menos un rol'
      return

    user =
      'username': username
      'password': password
      'profile':
        'name': name
      'roles': []

    roles.each (i) ->
      user.roles.push $(@).val()
      return

    Accounts.createUser user
    FlashMessages.sendSuccess 'Se ha creado un usuario'
    Router.go('/')

    return

和Accounts.onCreateUser

Accounts.onCreateUser (options, user) ->
  userProperties =
    profile: options.profile || {}
    roles: options.roles || []

  user = _.extend user, userProperties

  if !user.profile.name
    user.profile.name = user.username

  if !Meteor.users.find().count()
    user.roles.push 'admin', 'boss', 'specialist'

  user

1 个答案:

答案 0 :(得分:3)

如果您从客户端调用Accounts.createUser,这是预期的行为。正如the documentation中指出的那样:

  

在客户端上,此功能在成功完成时以新创建的用户身份登录。在服务器上,它返回新创建的用户ID。

在没有以新创建的用户身份登录的情况下插入用户的唯一方法是从服务器启动创建过程。您可以使用方法调用执行此操作。在submit回调中,将来自Accounts.createUser user的号码替换为:

Meteor.call 'insertUser', user

然后在服务器上,你可以实现这样的方法:

Meteor.methods
  'insertUser': (user) ->
    check user, Match.ObjectIncluding
      username: String
      password: String
      profile: name: String

    Accounts.createUser user

唯一的问题是您将用户的密码发送到服务器,这只有在使用SSL时才合理。即便如此,may not be a great idea ......

如果您不想传输密码,正如文档所指出的那样,您可以使用上面的示例创建用户,但您需要将password替换为email,然后像这样调用sendEnrollmentEmail

Meteor.methods
  'insertUser': (user) ->
    check user, Match.ObjectIncluding
      username: String
      email: String
      profile: name: String

    userId = Accounts.createUser user
    Accounts.sendEnrollmentEmail userId

然后,您需要在客户端上创建一个路由,在请求时,使用相应的令牌和新密码调用resetPassword。完成所有这些对您和用户来说都是更多的工作,但它是最安全的实现,可以满足您的要求。

相关问题