angularfire signedIn函数仅在页面刷新时进行评估?

时间:2015-11-30 19:52:22

标签: javascript angularjs firebase angularfire

当用户使用firebase和angular登录时,我隐藏了html登录和注册链接。 通过在Auth.js工厂中评估signedIn()函数,使用 ng-show ng-hide

<div class="navbar-collapse collapse navbar-responsive-collapse">
  <ul class="nav navbar-nav">
    <li ng-show="signedIn()"><a href="#/create">Create a Post</a></li>
  </ul>
  <ul class="nav navbar-nav navbar-right">
    <li ng-hide="signedIn()"><a href="#/register">Register</a></li>
    <li ng-hide="signedIn()"><a href="#/login">Login</a></li>
    <li ng-show="signedIn()" class="dropdown">
      <a href="" data-toggle="dropdown" class="dropdown-toggle">
         {{currentUser.profile.name}}
         <i class="mdi mdi-account"></i>
         <span class="caret"></span>
      </a>

这是我的Auth工厂服务:

angular.module('AuthService',[])
  .factory('Auth', function($firebaseAuth, $firebase){

  var ref = new Firebase("https://ngblogapp.firebaseio.com/");
  var authObj = $firebaseAuth(ref);
  var authData = authObj.$getAuth(); //firebase auth data when you login 

  var Auth = {
    user:{},

    createProfile: function(uid, user){
      var profile = {
        name: user.name,
        email: user.email,
        password: user.password,
      };

      var profileRef = $firebaseObject(ref.child('profile'));
        console.log("User Register Data : " + profileRef);
        return profileRef.$set(uid, profile);
    },
    login: function(user){
      return authObj.$authWithPassword(
        {email: user.email, password: user.password}
      ).then(function(authData) {
        console.log("Logged in as:", authData.uid);
      }).catch(function(error) {
        console.error("Authentication failed:", error);
      });
    },
    register: function(user){
      return authObj.$createUser({email: user.email, password: user.password})
      .then(function(userData) {
        console.log("User " + userData.uid + " created successfully!");
        return authObj.$authWithPassword({
          email: user.email,
          password: user.password,
        });
      }).then(function(authData) {
        console.log("Logged in as:", authData.uid);
      }).catch(function(error) {
        console.error("Error: ", error);
      });
    },
    logout: function(){
      authObj.$unauth(); //logout method
    },
    changePassword: function(user){
      return authObj.$changePassword({email: user.email, oldPassword: user.oldpass, newPassword: user.newpass});
    },
    signedIn: function(){
      var  signedin = authData === null ? false : true; //check to see if you are logged in
      console.log("signed in status is " + signedin);
      return signedin;
    }
  }; // end Auth
  if (authData) {
    console.log("Logged in as:", authData.uid);
  } else {
    console.log("Logged out");
  }
  return Auth;
});

我正在使用控制器返回我的signedIn函数。这是控制器:

app.controller('AuthController', function($scope, $location, Auth){

  $scope.register = function(user){
    Auth.register(user).then(function(){
      console.log("Register successfully!");
      $location.path("/");
    }, function(err){
      console.log("Error...");
    });
  };

上述代码仅在浏览器刷新时隐藏登录和注册链接 我的代码在angularfire中有什么错误,特别是在登录或签名后自动工作的signedIn方法?

1 个答案:

答案 0 :(得分:1)

所以我解决了这个问题。我的工厂有一个完全正常运行的注册,登录和注销方法,但firebase的getAuth只评估一次。要解决问题,我必须重新加载页面,但正确解决问题,我必须放置document.reload(); 在注册,登录和注销功能结束时。

这是预览。

login: function(user){
      return authObj.$authWithPassword(
        {email: user.email, password: user.password}
      ).then(function(authData) {
        console.log("Logged in as:", authData.uid);
        return authData; 
      }).catch(function(error) {
        console.error("Authentication failed:", error);
      }, location.reload()); //reload page after login
    },
    logout: function(){
      authObj.$unauth(); 
      location.reload(); //reload page
    },

location.reload()方法将在注销或登录后重新加载文档,并且firebase getAuth()将正确评估,因此允许我使用ng-hide或ng-show进行隐藏和在我的导航栏上显示我的html模板以显示日志状态。

现在,当注销时,导航栏将显示注册和登录链接

enter image description here

登录或注册后,您会在导航栏中看到退出选项。

enter image description here