AngularJS和Firebase身份验证在第二次点击后调用路由

时间:2016-11-03 17:56:12

标签: javascript angularjs firebase routing firebase-authentication

我正在使用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('/');
    });
}

1 个答案:

答案 0 :(得分:0)

所以我在阅读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);
    });