0

My logout function, linked to a logout button is:

$scope.logoutUser = function() {
  var ref = new Firebase("https://buzzmovieionic.firebaseio.com");
  ref.unauth();
  console.log(ref.getAuth);
  $state.transitionTo('login');
}

When I click logout, it prints this to the console:

function (){x("Firebase.getAuth",0,0,arguments.length);return this.k.P.we()}

I am checking for authData in my other controller with:

CONTROLLER:

.controller('SearchCtrl',
    function ($scope, $http, Movie, $state, UsersRef, AuthData, $timeout) {
      $scope.$on('$ionicView.enter', function () {
        if (!AuthData) {
          console.log("Auth data null!");
          swal("Unauthorized", "You are not logged in", "error");
          $state.transitionTo('login');
        } else {
          console.log("Auth data found: " + AuthData);
          //do stuff
        }
    });
})

FACTORY:

.factory("AuthData", [
    function () {
      var ref = new Firebase("https://buzzmovieionic.firebaseio.com");
      var data = null;
      ref.onAuth(function (authData) {
        if (authData) {
          data = authData;
        }
      });
      return data;
    }
  ])

If I logout, then go back to the page linked to SearchCtrl by changing the URL, it still says it found the authData.

However, if I try and go to the search page the FIRST time I open the app, before anybody has logged in, it gives me the right error message and exits out to the login page.

How can I ensure the user can't go back into the app after logging out?

2
  • You're missing parentheses after getAuth: console.log(ref.getAuth()); Commented Apr 30, 2016 at 3:52
  • @FrankvanPuffelen added them, now it prints null after logout. Why does it then say Auth Data was found. Commented Apr 30, 2016 at 4:51

2 Answers 2

2

Welcome to async programming 101.

Firebase's onAuth methods listens for changes on auth state. When the auth state changes, the callback method you provide is invoked. But while it's waiting for auth state changes, your other code continues to run.

It most easy to see this if you add some log statements to your code:

.factory("AuthData", [
    function () {
      var ref = new Firebase("https://buzzmovieionic.firebaseio.com");
      var data = null;
      console.log('before onAuth');
      ref.onAuth(function (authData) {
        console.log('in callback');
        if (authData) {
          data = authData;
        }
      });
      console.log('after onAuth');
      return data;
    }
  ])

The output is going to be:

before onAuth

after onAuth

in callback

Which is likely not what you expected when you wrote this code.

The simplest way to fix this in your code is to use the synchronous ref.getAuth() method:

.factory("AuthData", [
    function () {
      var ref = new Firebase("https://buzzmovieionic.firebaseio.com");
      return ref.getAuth();
    }
  ])

But you're going to run into this asynchronicity problem quite often. I highly recommend using and studying AngularFire instead of reinventing the wheel.

Sign up to request clarification or add additional context in comments.

Comments

0

You are never cleaning data inside AuthData so it will always have data after the first guy logs in. I'm not familiar with Firebase but you need something like this in your AuthData factory:

.factory("AuthData", [
function () {
  var ref = new Firebase("https://buzzmovieionic.firebaseio.com");
  var data = null;
  ref.onAuth(function (authData) {
    if (authData) {
      data = authData;
    }
    else
      data = null;
  });
  return data;
}
])

5 Comments

onAuth() docs onAuth() should be a listener, so ideally data should update whenever auth status changes.
then perhaps all you miss is an else statement. I've updated my code so you could try
could you put it in a plunkr?
It's an ionic project found here It's hosted at this link
If i'm not mistaken there are plunkr-like sites for Ionic projects. can't recall the name though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.