I am following the AngularFire tutorial on thinkster. The $simpleLogin service has been deprecate, so I changed some of the code.
The factory:
app.factory('Auth', function ($firebaseSimpleLogin, FIREBASE_URL, $rootScope, $location){
var ref = new Firebase(FIREBASE_URL);
var Auth = {
register = function(){
//...
},
resolveUser = function(){
//...
},
signedIn = function(){
var authData = ref.getAuth();
if (authData) {
console.log("User is signed in");
return true;
} else {
console.log("User is not signed in");
return false;
}
}
The controller:
app.controller('NavCtrl', ['$scope', '$location', 'Post', 'Auth', 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.signedIn = Auth.signedIn();
$scope.logout = Auth.logout;
}]);
app.config(function($routeProvider){
$routeProvider
.when('/', {
templateUrl: 'views/posts.html',
controller: 'PostsCtrl'
})
.when('/posts/:postId', {
templateUrl: 'views/showpost.html',
controller: 'PostViewCtrl'
})
.when('/register', {
templateUrl: 'views/register.html',
controller: 'AuthCtrl',
resolve: {
user: function(Auth){
return Auth.signedIn();
}
}
})
.when('/login', {
templateUrl: 'views/login.html',
controller: 'AuthCtrl',
resolve: {
signedIn: function(Auth){
return Auth.signedIn();
}
}
})
.otherwise({
redirectTo: '/'
});
Relevant part of the template:
<ul class="nav navbar-nav navbar-right">
<li ng-show="signedIn">
<a ng-href="#" ng-click="logout()">Logout</a>
</li>
<li ng-hide="signedIn">
<a href="#/register">Register</a>
</li>
<li ng-hide="signedIn">
<a href="#/login">Login</a>
</li>
The problem is the signedIn variable doesn't get updated when I login in, so the register and Login links are still visible to the user while the logout link is not shown.
Finally figure out a solution,
In the controller:
In the factory:
So I think it's the
$timeoutthat makes the difference?