【问题标题】:How does firebase-authentication workfirebase 身份验证如何工作
【发布时间】:2016-12-04 06:27:36
【问题描述】:

来自 firebase 3 文档 https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user

的代码
    firebase.auth().onAuthStateChanged(function(user){
          if (user) {
            // If there's a user take him to the home page.
            this.rootPage = HomePage;
          } else {
            // If there's no user logged in send him to the LoginPage
            this.rootPage = LoginPage;
          }
  });

观察者中的user参数如何设置,上面的代码和下面的代码有什么不同?

教程代码@http://javebratt.com/firebase-3-email-auth/

firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            // If there's a user take him to the home page.
            this.rootPage = HomePage;
          } else {
            // If there's no user logged in send him to the LoginPage
            this.rootPage = LoginPage;
          }
    });

我问的原因是,我浏览了 ionic 2 和 firebase 3 的教程,firebase.auth().onAuthStateChanged() 的实现明显不同于 firebase 3 官方文档中的内容

【问题讨论】:

    标签: typescript ionic-framework firebase ionic2 firebase-authentication


    【解决方案1】:

    两者的相似之处在于第一个使用回调函数,第二个使用arrow function。例如:这些是相同的(作为示例):

    var a = a.map(function(s){ return s.length });
    var b = a.map( s => s.length );
    

    当用户通过 firebase3 进行身份验证时,它将在回调或箭头函数中传回一些数据。此数据可在您的示例中的用户变量下访问。您可以执行 console.log(user) 来查看传递了哪些对象(例如身份验证 ID、提供者、电子邮件...)。

    希望这会为您指明正确的方向。

    【讨论】:

      【解决方案2】:

      用户参数设置为 var user = rootRef.getAuth();

      这是我使用它的方式。

      var rootRef = new Firebase('https://yourapp.firebaseio.com');
      
      // Check the current user login status and redirect if not logged in
      var user = rootRef.getAuth();
      if (user) {
         var user = rootRef.getAuth();
         var userRef = rootRef.child('users').child(user.uid);
         ... do something with the logged in user...
      }
      

      希望这会有所帮助。

      【讨论】:

      • 感谢@MattHannam,您的解释似乎是针对最近弃用的 firebase 2
      猜你喜欢
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 2011-11-19
      相关资源
      最近更新 更多