简体   繁体   English

Polymer firebase。通过密码和电子邮件进行身份验证

[英]Polymer firebase.. Authentication with password and email

I have a function that logs in a user successfully. 我有一个可以成功登录用户的功能。

_login: function() {
   var email = this.$.emailvalue.value;
   var password = this.$.passwordvalue.value;

   return this.$.authenticate.signInWithEmailAndPassword(email, password).then(function() {
      // Sign-in successful.
      //this._animateView(); **returns undefined**
    }, function(error) {
      // An error happened.
    //     // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message; 
        console.log(errorMessage);
        // this._animateErrorView(); **returns undefined**
    });

  },

What I want is to call a function this._animateView(); 我想要的是调用一个函数this._animateView(); when a user logs in successfully and this._animateErrorView() when there is an error. 当用户成功登录时以及this._animateErrorView()出现错误时。

If I try to do that It returns undefined.. How do I solve this issue 如果我尝试这样做,它将返回undefined。

The meaning of this is different in the callback function. 的意思this是在回调函数不同。

You can fix this in many ways, but here are two. 您可以通过多种方式解决此问题,但这有两种。

capture the value of this in another variable 在另一个变量中捕获此值

You cannot prevent this from getting a new value, since that's simply how JavaScript closures work. 您不能阻止this获得新的价值,因为这正是JavaScript闭包的工作方式。 But what you can do is define a different value with the value that you need. 但是您可以做的是用所需的定义一个不同的值。 A idiomatic name for the variable is self , although I personally prefer something more descriptive: 变量的惯用名称是self ,尽管我个人更喜欢更具描述性的名称:

_login: function() {
   var email = this.$.emailvalue.value;
   var password = this.$.passwordvalue.value;
   var self = this;    
   return this.$.authenticate.signInWithEmailAndPassword(email, password).then(function() {
        self._animateView();
    }, function(error) {
        var errorCode = error.code;
        var errorMessage = error.message; 
        console.log(errorMessage);
        self._animateErrorView();
    });
  }

use fat arrow functions 使用粗箭头功能

ES6 defines a new way to declare functions with so-called fat arrows ( => ). ES6定义了一种使用所谓的胖箭头=> )声明函数的新方法。 Aside from being slightly less code, these ensure the value of this in the lambda/callback remains unchanged. 除了减少一些代码外,这些还确保在lambda / callback中的this值保持不变。 So 所以

_login: function() {
   var email = this.$.emailvalue.value;
   var password = this.$.passwordvalue.value;
   return this.$.authenticate.signInWithEmailAndPassword(email, password).then(() => {
        this._animateView();
    }, (error) => {
        var errorCode = error.code;
        var errorMessage = error.message; 
        console.log(errorMessage);
        this._animateErrorView();
    });
  }

In addition to @Frank van Puffelen answer, there is also another approach which is used by polymer dev team all over the place: 除了@Frank van Puffelen的答案外,各地的聚合物开发团队还使用了另一种方法:

_login: function() {
   var email = this.$.emailvalue.value;
   var password = this.$.passwordvalue.value;

   return this.$.authenticate.signInWithEmailAndPassword(email, password).then(function() {
      // Sign-in successful.
      //this._animateView(); **returns undefined**
    }.bind(this), function(error) {
      // An error happened.
    //     // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message; 
        console.log(errorMessage);
        // this._animateErrorView(); **returns undefined**
    }.bind(this));

  },

so basically you add .bind(this) to every callback at the end. 因此,基本上,您将.bind(this)添加到最后的每个回调中。

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

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