繁体   English   中英

更新MeteorJS帐户密码插件

[英]Updating MeteorJS accounts-password plugin

我目前正在使用通过命令安装的插件“ accounts-password”在个人资料页面上工作

meteor add accounts-password

我已经运行了程序等等,并且能够添加数据; 但是,当我尝试使用浏览器在浏览器中调用或显示数据时

{{ currentUser.email }}

{{ currentUser.password }}

它不显示,但是当我打电话时

{{ currentUser.username }}

它工作正常。 我试图通过find()。fetch();访问数据库; 这就是我所看到的。

id: "GP26SF2F8jmpqQuvT"
emails: Array[1]
      0: Object
        address: "testing@gmail.com"
      verified: false
      __proto__: Object
      length: 1
__proto__: Array[0]
username: "testing"
__proto__: Object

根据安排,我应该将电子邮件称为

{{ currentUser.emails.0.address }}

我也没有在数据中看到密码,有没有办法找回它? 实际上,我的目的是在用户想要更改密码或电子邮件地址的情况下更新Meteor.users。 谢谢。

默认情况下, accounts-password不会发布文档的password字段。 出于安全原因,这是可以预期的!

关于电子邮件: accounts程序包允许应用程序向用户添加多封电子邮件。 这就是为什么有一系列电子邮件的原因。

而不是做

{{ currentUser.emails.0.address }}

您可以做的是向模板添加一个助手:

Template.myTemplate.helpers({
   email: function(){
       return Meteor.user().emails[0].address;
   }
}); 

然后,您可以在模板中使用它:

{{ email }}

实际上,我的目的是在用户想要更改密码或电子邮件地址的情况下更新Meteor.users。 谢谢。

我想说,几乎没有理由将用户密码发布给客户端。 这是巨大的安全风险。 accounts-password包已处理了常见的用例,因此您可以只在客户端上使用Accounts.changePassword()来允许用户更改其密码。

如果要允许用户更改其电子邮件,则要使用方法 客户端可以调用一个方法,但是代码是在服务器上执行的。 执行后,服务器将响应返回给客户端。 有点像HTTP的工作方式。

在这种情况下,客户端可以调用名为changeEmail的方法,该方法尝试更改用户的电子邮件。 如果所有检查均通过,则服务器将更改用户的电子邮件并返回响应,例如"success" ,否则返回"fail" 代码如下所示:

if(Meteor.isClient){
    Meteor.call('changeEmail', newEmail, function(error, response){
        if(error){
            console.error(error);
        } else {
            console.log(response);
        }
    });
}

if(Meteor.isServer){
    Meteor.methods({
        changeEmail: function(newEmail){
            Accounts.addEmail(this.userId, newEmail, false);
            var userEmails = Meteor.users.findOne(this.userId, {fields: {emails: 1}}).emails;
            if(userEmails.length > 1){
                Accounts.removeEmail(this.userId, userEmails[0].address);
                Accounts.sendVerificationEmail(this.userId, newEmail);
                return "success";
            } else {
                throw new Meteor.Error('fail', 'email not correct!');
            }
        }
    });
}

如果你不熟悉的方法,你可以读取这个由流星教程或文章。 另外,我的代码可能不是100%起作用的,这只是一个例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM