0

Based on the authentication data from Firebase when logging in, I want to route to a certain view after the login was successful.

 app.controller('PageCtrl', function ($scope, $location, $http ) {

  $scope.logIn = function(){
      var email = $('#login-email').val();
      var password = $('#login-password').val(); 

      myDataRef.authWithPassword({
        email    : email,
        password : password
      }, function(error, authData) {
        if (error) {         
          console.log("Login Failed!", error);
        } else {
          console.log("Authenticated successfully with payload:", authData);
          email = myDataRef.getAuth().password.email;
          $('#userinfo').html(email);
          $('#logoutButton').show();
        }
      });
      $location.path('/form'); //route to form 
  }
}

By using $location.path('/form'); The view will change, but this happens before the login was determined to be successful or unsuccessful. How can I route to one view after the login was successful, and another view if the login was unsuccessful? I placed the $location.path('/form'); inside the if else error logic but it doesn't work.

user3646037
  • 293
  • 1
  • 6
  • 16

1 Answers1

1

What does the authWithPassword method look like?

It is probably returning a promise and you should use the .then success/error callback to enforce the flow you are looking for. Something like this should work.

ref.authWithPassword({
  "email": "bobtony@firebase.com",
  "password": "correcthorsebatterystaple"
}, function(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
    $location.path("/loginFailedPathHere");
  } else {
    console.log("Authenticated successfully with payload:", authData);
    $location.path("/form");
    $scope.$apply()
  }
});
BBauer42
  • 3,549
  • 10
  • 44
  • 81
  • [This is the authWithPassword method](https://www.firebase.com/docs/web/api/firebase/authwithpassword.html), what you provided doesn't seem to work – user3646037 Feb 03 '16 at 20:11
  • I updated my answer based on your comment. You say you tried this and it didn't work? What error did you see? Also, have you seen [this SO post](http://stackoverflow.com/questions/32894504/ref-authwithpassword-is-not-a-function-in-firebase)? – BBauer42 Feb 03 '16 at 20:38
  • I tried your updated version, there is no error, the routing is not performed. – user3646037 Feb 03 '16 at 22:00
  • 1
    Seems like a routing problem, not an issue with the firebase method. Try looking for [posts like this](http://stackoverflow.com/questions/11784656/angularjs-location-not-changing-the-path). – BBauer42 Feb 04 '16 at 12:56
  • Great, I updated the answer to include the $scope.$apply(). – BBauer42 Feb 04 '16 at 15:42