向Meteor用户帐户添加更多字段

时间:2014-05-07 01:52:56

标签: meteor user-accounts

我正在使用mrt add accounts-ui-bootstrap-dropdownmrt add accounts-password来获取在我的应用上运行的简单登录页面。

帐户用户给了我一个包含id,createdAt,电子邮件等的好哈希

如果我想在此哈希中添加其他字段,以便稍后可以使用它们,我该怎么做?例如,我想要输入他们的名字和姓氏:

“given_name”:“John”,“surname”:“Doe”

6 个答案:

答案 0 :(得分:40)

用户是流星中的特殊对象;您不想在用户中添加字段,但需要在用户个人资料中添加字段。

来自doc:

By default the server publishes username, emails, and profile.

如果您想在创建帐户时添加姓氏等属性,则应在Account.onCreateUser服务器端钩子中使用http://docs.meteor.com/#accounts_oncreateuser

Accounts.onCreateUser(function(options, user) {
    //pass the surname in the options

    user.profile['surname'] = options.surname

    return user
}

如果您想在之后更新用户,可以通过以下方式从客户端进行更新:

Meteor.users.update({_id:Meteor.user()._id}, { $set: {what you want to update} });

默认情况下,用户群将允许(当前用户可以自行更新)。如果您不信任您的用户并希望确保所有内容都正确更新,您也可以禁止客户端的任何更新,并通过Meteor.call()进行更新,然后继续进行检查服务器端。但这很难过。


修改:

如评论中所述,通过标准帐户添加选项-ui是不可能的。您只能在注册后更新用户。要在订阅时添加选项,您必须自己创建表单。

我不会通过编写html标记来侮辱你,但是这是你在提交事件之后(以及在各种检查之后)想要拥有的内容:

var options = {
    username: $('input#username')[0].value,
    emails: [{
        address: $('input#email')[0].value,
        verified: false
    }],
    password: $('input#password')[0].value,
    profile: {
        surname: $('input#surname')
    },
};
Accounts.createUser( options , function(err){
    if( err ) $('div#errors').html( err.message );
});

您只需要基于帐户的套餐;不是帐户 - ui。

使用社交网络登录是蛋糕:

Meteor.loginWithFacebook({
    requestPermissions: ['email', 'user_birthday', 'user_location']
}, function(error){loginCallBack(error);});

关于ram1的答案:

这不是流星的工作方式。你没有" POST"表单。您希望通过websocket完成所有客户端/服务器通信。相当于你所说的是制作一个" Meteor.call(' myserverfunction',myarguments,mycallback)"客户端的服务器方法,并传递您希望服务器使用的参数。

但这不是你获得最好的流星的方式。您希望与之合作的理念是:

  1. 您从服务器获得的本地迷你mongo中有数据
  2. 您在本地更新基础/视图中的数据
  3. meteor将他的魔力传递给服务器
  4. 服务器可以回答:确定,保存更新,这对您来说是无缝的。或回答:nop!反转更改(您可以实现错误通知系统)
  5. (它可以回答否,因为您没有权限更新此字段,因为此更新违反了您设置的规则...)

    您所做的只是在数据库服务器端设置权限和控制。这样,当一个诚实的客户进行更新时,他会立即看到结果;在将其推送到服务器并发送给其他客户端之前的方式。这是 延迟补偿 ,是流星的七大原则之一。

    如果你通过Meteor.call修改数据,你会这样做:

    1. 向服务器发送更新
    2. 服务器检查并更新基础
    3. 服务器将更新发送给客户端(包括您)
    4. 您的本地基础更新和您的观看次数更新=>你看到了你的更新
    5. =>这就是你昨天的应用程序; meteor允许你构建一个今天的应用程序。不要应用旧的食谱:)

答案 1 :(得分:13)

接受的答案如何正确,但WHERE是过时的信息。 (是的,作为对答案的评论会更好,但我还不能这样做。)

来自Meteor 1.2 documentation

  

将自定义数据存储到Meteor.users集合的最佳方法是在用户文档中添加一个新的唯一命名的顶级字段。

关于使用Meteor.user.profile存储自定义信息:

  

不要使用个人资料

     

有一个诱人的现有字段称为配置文件,由其添加   新用户注册时的默认值。这个领域是历史性的   旨在用作用户特定数据的便笺簿 - 也许   他们的形象头像,名称,介绍文本等。因此,   每个用户的配置文件字段可由该用户自动写入   来自客户。它也会自动发布到客户端   那个特定的用户。

基本上,在配置文件字段中存储名称,地址,dob等基本信息可能没什么问题,但是不能存储除此之外的任何内容,因为默认情况下,它可以写入客户端易受恶意用户攻击。

答案 2 :(得分:10)

我遇到了同样的问题,并设法只使用Accounts.createUser

Accounts.createUser({
    email: email,
    password: password,
    profile: {
            givenName: 'John',
            surname: 'Doe',
            gender: 'M'
        }
}

这很简单,很有效。只需在配置文件部分添加所需的变量即可。希望它可以帮到某人。

答案 3 :(得分:1)

我最终使用https://atmospherejs.com/joshowens/accounts-entry提供了extraSignUpFields config选项。

答案 4 :(得分:1)

从文档(https://github.com/ianmartorell/meteor-accounts-ui-bootstrap-3/blob/master/README.md):

自定义注册选项

您可以定义其他输入字段以显示在注册表单中,您可以决定是否将这些值保存到用户文档的配置文件对象中。使用Accounts.ui.config指定一个字段数组,如下所示:

Accounts.ui.config({
    requestPermissions: {},
    extraSignupFields: [{
        fieldName: 'first-name',
        fieldLabel: 'First name',
        inputType: 'text',
        visible: true,
        validate: function(value, errorFunction) {
          if (!value) {
            errorFunction("Please write your first name");
            return false;
          } else {
            return true;
          }
        }
    }, {
        fieldName: 'last-name',
        fieldLabel: 'Last name',
        inputType: 'text',
        visible: true,
    }, {
        fieldName: 'gender',
        showFieldLabel: false,      // If true, fieldLabel will be shown before radio group
        fieldLabel: 'Gender',
        inputType: 'radio',
        radioLayout: 'vertical',    // It can be 'inline' or 'vertical'
        data: [{                    // Array of radio options, all properties are required
            id: 1,                  // id suffix of the radio element
            label: 'Male',          // label for the radio element
            value: 'm'              // value of the radio element, this will be saved.
          }, {
            id: 2,
            label: 'Female',
            value: 'f',
            checked: 'checked'
        }],
        visible: true
    }, {
        fieldName: 'country',
        fieldLabel: 'Country',
        inputType: 'select',
        showFieldLabel: true,
        empty: 'Please select your country of residence',
        data: [{
            id: 1,
            label: 'United States',
            value: 'us'
          }, {
            id: 2,
            label: 'Spain',
            value: 'es',
        }],
        visible: true
    }, {
        fieldName: 'terms',
        fieldLabel: 'I accept the terms and conditions',
        inputType: 'checkbox',
        visible: true,
        saveToProfile: false,
        validate: function(value, errorFunction) {
            if (value) {
                return true;
            } else {
                errorFunction('You must accept the terms and conditions.');
                return false;
            }
        }
    }]
});

答案 5 :(得分:1)

官方Meteor指南提供了一个全面的答案,其中包含示例代码:

  

将自定义数据存储到Meteor.users集合的最佳方法是在用户文档中添加一个新的唯一命名的顶级字段。

https://guide.meteor.com/accounts.html#custom-user-data