Twitter 或普通帐户用户名冲突 - Meteor

Twitter or normal account username conflict - Meteor

用户可以使用他们的电子邮件或推特创建一个帐户。下面代码的问题是,它不允许拥有普通帐户的用户创建 post.

Posts.insert({
      text: text,
      createdAt: new Date(),
      owner: Meteor.userId(),
      username: Meteor.user().username,
      twitname: Meteor.user().profile.name,
      upvoters: [],
      votes: 0,
      commenters: [],
      comments: 0,    
}); 

如果我擦除

      twitname: Meteor.user().profile.name,

这两个帐户都可以再次创建 post,但它不会显示由 Twitter 帐户创建的 post 的用户名。

我找到了解决办法。希望这对某人有所帮助。

我们所做的是告诉 "username" 从一个或另一个获取数据,如下所示。

服务器内部,Meteor.methods:

username: function(){
   return Meteor.user().username || Meteor.user().profile.name
}

我们的插入方法:

Posts.insert({
    text: text,
    createdAt: new Date(),
    owner: this.userId,
    username: Meteor.user().username,
});

里面 HTML:

{{username}}

PS:我已经安装了使用 Google、Twitter 和 FB 登录。上面的解决方案适用于所有这些。

编辑:

我发现使用 onCreateUser 更好

示例:

Accounts.onCreateUser(function(options,user){
    if (typeof(user.services.facebook) != "undefined") {
        username: user.services.facebook.name;
    }

    if (typeof(user.services.google) != "undefined") {
        user.username = user.services.google.name;
    }

    //etc.
})

这样一来,您就不必检查是哪个服务,也不必为所有服务编写任何其他内容,因为它总是 "username"。您只需将正确的数据插入同一个地方。