Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thinkster.io angular chapter 6 error - auth.logout() not working

If someone who have gone through thinkster.io augularjs tutorial would be familiar with the below code. I am facing the below error.

<ul class="nav navbar-nav navbar-right" ng-show="signedIn()">
    <li>
        <a href="#" ng-click="logout()">Logout</a>
    </li>
</ul>

First the logout button was not showing up. after adding single quotes it showed up

<ul class="nav navbar-nav navbar-right" ng-show="'signedIn()'">

But as i click on logout the user is not getting logged out.

nav.js code

app.controller('NavCtrl', function ($scope, $location, Post, Auth) {
    $scope.post = {url: 'http://', title: ''};

    $scope.submitPost = function () {
      Post.create($scope.post).then(function (ref) {
        $location.path('/posts/' + ref.name());
        $scope.post = {url: 'http://', title: ''};
      });
    };

    $scope.logout = function () {
      Auth.logout();
    };

});

services/auth.js code

app.factory('Auth', function($firebaseSimpleLogin, FIREBASE_URL, $rootScope){
    var ref = new Firebase(FIREBASE_URL);

var auth = $firebaseSimpleLogin(ref);

var Auth = {
    register: function(user){
        return auth.$createUser(user.email, user.password);
    },
    signedIn: function(){
        return auth.user !== null;
    },
    logout: function(){
        auth.$logout();
    }

};
$rootScope.signedIn = function(){
    return Auth.signedIn();
};
return Auth;

} );

like image 634
Anil S Avatar asked May 24 '26 09:05

Anil S


1 Answers

Since I came across this issue myself and checked the GitHub issues, here's what you need to do.

Create a login method on your Auth service, like so:

login: function(user) {
    return auth.$login('password', user);
}

Inside your auth controller, you need to login after you register, like so:

Auth.register($scope.user).then(function (authUser) {
    Auth.login($scope.user); // user is now logged in
});

Since your login method returns a promise, you can do whatever you like when the user has logged in.

like image 96
Daniel Avatar answered May 27 '26 04:05

Daniel