$rootScope.currentUser is null on refresh

All we need is an easy explanation of the problem, so here it is.

I was able to get the firebaseSimpleLogin working and storing the current user in the rootScope. When I goto another page and come back to the feed page everything loads, but when I refresh $rootScope.currentUser is null. Has anyone else had this issue?

I have this piece of code in my app.run function:

$rootScope.$on('$firebaseSimpleLogin:login',function(e, auth){
   $rootScope.currentUser = User.find(auth.id);
});

And this is my FeedCtrl that is trying to load in the user’s posts

app.controller('FeedCtrl',['$scope', '$rootScope','User','Auth','Post',function($scope,     $rootScope,User,Auth,Post){
 populateFeed();
 console.log($rootScope.currentUser);

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


$scope.savePost = function(){
  Post.create($scope.post).then(function(post){
    $scope.post.text = "";
  });
};

function populateFeed(){
   $scope.posts = {};
   angular.forEach($rootScope.currentUser.posts,function(id){
    $scope.posts[id] = Post.find(id);
   });
}

}]);

My Main app Module

var app = angular.module('myapp',['ui.router','firebase']);

app.config(function($stateProvider, $urlRouterProvider){
  $stateProvider
.state('/',{
  url: '/',
  controller: 'MainCtrl',
  templateUrl: 'views/index.html'
})
.state('feed',{
  url: '/feed',
  controller: 'FeedCtrl',
  templateUrl: 'views/feed.html',
  authenticate: true
})
.state('login',{
  url: '/login',
  controller: 'LoginCtrl',
  templateUrl: 'views/login.html'
})
.state('signup',{
  url: '/signup',
  controller: 'LoginCtrl',
  templateUrl: 'views/signup.html'
})
.state('settings',{
  url: '/settings',
  controller: 'SettingsCtrl',
  templateUrl:"views/settings.html"
})
.state('profile',{
  url: '/:slug',
  controller: 'ProfileCtrl',
  templateUrl: 'views/profile.html'
})
.state('profile-about',{
  url: '/:slug/about',
  controller: 'ProfileCtrl',
  templateUrl: 'views/profile.html'
});

$urlRouterProvider.otherwise('/');
})
.constant('FIREBASE_URL','https://{FIREBASE}.firebaseio.com/')
.run(function($rootScope, $state,FIREBASE_URL, $firebaseSimpleLogin, User, Auth){
$rootScope.$on('$stateChangeStart',function(event, next){
  if(next.authenticate && !User.getCurrentUser()){
     $state.go('login');
  }
 });

 $rootScope.$on('$firebaseSimpleLogin:login',function(e, auth){
   $rootScope.currentUser = User.find(auth.id);
  });

 $rootScope.$on('$firebaseSimpleLogin:logout',function(){
  delete $rootScope.currentUser;
  $state.go('login');
 });

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

});

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

If you by ‘refresh’ mean that you hit F5 in the browser that will wipe your $rootscope from memory and you will get null back as you experience (basically your entire app will be ‘restarted’).

In order to persist values you either need to store them in a cookie or use localStorage/sessionStorage. For example, instead of using $rootscope.currentUser use $window.sessionStorage.currentUser.

Method 2

To store it in localStorage:
$window.localStorage.setItem(“currentUser”, currentUser);

or store in json format:
$window.localStorage.setItem(“currentUser”, angular.toJson(currentUser));

To get it form there:
var currentUser = $window.localStorage.getItem(“currentUser”);

or if you saved it using the second way:
var currentUser = JSON.parse($window.localStorage.getItem(“currentUser”));

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply