【问题标题】:AngularJS and Firebase Authentication invoke Routing after the second clickAngularJS 和 Firebase 身份验证在第二次点击后调用路由
【发布时间】:2017-03-17 10:27:58
【问题描述】:

我正在使用 Angular 进行 Firebase 身份验证,我遇到了一个问题,$location.path 函数仅在我第二次点击后才调用,第一次,我运行这个 loginVerify,Firebase 将访问者身份验证为用户,但它没有路由到我的联系页面,我第二次单击时,Firebase 再次将访问者验证为用户并将用户路由到登录页面。谁能帮我找出原因?非常感谢!

源码:https://github.com/ptchiangchloe/Meet-Up-Event-Planner

这是我的现场版本:https://meet-up-event-planner-d45a4.firebaseapp.com/#!/

vm.loginVerify = function() {
    firebase.auth().signInWithEmailAndPassword(vm.email, vm.password))
    .then(function(response) {
      console.log("Success!", response); // the first I run this fn, firebase authenticate the visiter is a user but it didn't route to my contact page
      $location.path('/contacts');// the second time I click, firebase authenticate the visiter is a user again and route the user to my contact page
    }).catch(function(error) {
      var errorCode = error.code;
      var errorMessage = error.message;
      console.log(errorCode);
      console.log(errorMessage);
      $location.path('/');
    });
}

【问题讨论】:

    标签: javascript angularjs firebase routing firebase-authentication


    【解决方案1】:

    所以我在阅读How to redirect after a user signs in with Firebase authentication? 后发现了问题所在 问题是我不需要重定向,相反,我需要更改我的应用程序的状态,因为它是一个单页应用程序。我不完全理解国家是如何运作的。

    我可以简单地在成功回调中使用 JavaScript 设置窗口位置,而不是使用 Angularfire。有人告诉我,如果可能的话,不要将 jQuery 与 Angular 混合使用,否则你会遇到错误。所以我想这可能是以前的问题。

    这是我的新代码。

       firebase.auth().signInWithEmailAndPassword(vm.email, vm.password)
        .then((firebaseUser) => {
            return firebaseUser
        })
        .then((firebaseUser) => {
          firebase.auth().onAuthStateChanged(function(firebaseUser) {
            if (firebaseUser) {
              window.location = '/#!/contacts'
            }
          });
        })
        .catch(function(error) {
          var errorCode = error.code;
          var errorMessage = error.message;
          console.log(errorCode);
          console.log(errorMessage);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2019-08-15
      • 1970-01-01
      相关资源
      最近更新 更多