0

I watched a video on lynda, but unfortunately it's not on the latest version of the API.

The video shows how to use firebaseSimpleLogin, but this was changed to authWithPassword if I want a email & password type of authentication.

I can login, logout and register but the

PROBLEM is: I can't verify if the user is logged in or logged out.

here is my code below:

The Factory

    GlobalCtrl.factory('Auth', ['$firebaseAuth', function($firebaseAuth){
    var ref = new Firebase("https://taxiq-app.firebaseio.com"); //call on firebase database
    //var objects = $firebase(ref) // reference database
    return $firebaseAuth(ref);
}]);

The controller

taxiGlobalCtrl.controller('LoginController',['$scope','$firebase','Auth' ,'$location', function($scope, $firebase,Auth, $location){

    $scope.login = function(){

         Auth.$onAuth(function(authData) {
             $scope.authData = authData;
             console.log($scope.authData);
         });

        Auth.$authWithPassword({
              email: $scope.username,
              password: $scope.password
            }).then(function(authData) {
              console.log("Logged in as:", authData.uid);
              $location.path('/tickets');
            }).catch(function(error) {
              console.error("Authentication failed:", error);
              $scope.loginError = "loging error";
            });
    }

}]);

The naV that will show logout after logged in

<div>
    <a class="navbar-brand" ng-href="#">Tiketa</a>
    <ul class="nav navbar-nav">
        <li ng-hide="authData"><a ng-href="#/login">Login</a></li>
        <li ng-hide="authData"><a ng-href="#/register">Register</a></li>
        <li ng-show="authData"><a ng-href="#/tickets">Tickets</a></li>
        <li ng-show="authData"><a ng-href="#" ng-click="auth.$unauth()">logout</a></li>
    </ul>

note: I'm fresh from online resources about angular and firebase,... I'm a noob

sbolel
  • 3,486
  • 28
  • 45
  • `ng-click="auth.$unauth()"` isn't doing anything. `auth` is not in the scope (it's not the same thing as Auth). Although this is not a great way of doing what you're trying to accomplish, you would add `$scope.auth = Auth;` in your controller (for logout, not login), and that ng-click should work. Also, ng-show/ng-hide is not as good as `ng-if` for displaying things that are based on auth. ng-if actually removes something from the DOM. see http://stackoverflow.com/questions/21869283/when-to-favor-ng-if-vs-ng-show-ng-hide – sbolel Mar 03 '15 at 19:38
  • As for verifying whether a user is logged in or not, in your `.then()`, you can do `$rootScope.authData = authData;` and then check `$rootScope.authData.$getAuth()`. Again, this is **not** a good way of doing this. You'd be much better off putting everything into an authentication service or user factory. – sbolel Mar 03 '15 at 19:43
  • This might be too much for you right now, but my friend built an excellent library for managing users with Angular + Firebase called FireAdmin, https://github.com/prescottprue/FireAdmin -- it might help ease some of your pain. – sbolel Mar 03 '15 at 19:46

0 Answers0