Can't refresh datas on side-menu after login/logout

Summary

Using ionic creator, I have an issue when login/logout my side menu informations keep previous user’s informations.

Using ionic creator, I have an issue when login/logout, my side menu informations (avatar, user name…) keep the previous user’s informations (I’m switching between two account to test). I don’t know how to refresh or delete all informations related to the previous user, to refresh my side menu for the next one. I can see in the console that the right values are loaded, but they don’t erase the previous one in the menu.

Any suggestions ?

Here is my code :

// SIGNOUT FUNCTION in the controller

$scope.signOut = function(){ 
   firebase.auth().signOut().then(function() {
            
            // This is to try to "clean" "clear" all informations of the previous user
            $ionicLoading.show({
            template: 'Logging out....'
            });
    
                $timeout(function () {
                $ionicLoading.hide();
                $ionicHistory.clearCache();
                $ionicHistory.clearHistory();
                $ionicHistory.nextViewOptions({
                disableBack: true,
                historyRoot: true
                });
            $state.go("connexion");
        }, 30);         
    });
};

// MAJ DOM (menu) FUNCTION in the controller
// function used for refresh the menu informations (avatar, displayname...)

$scope.majDom = function()
{
    //$state.reload();
    var user = firebase.auth().currentUser;
    var uid;
    $scope.userData = user;

    if (user !== null) {
        $scope.userData.displayName = user.displayName;
        $scope.userData.email = user.email;
        $scope.userData.avatar = user.photoURL;
        uid = user.uid;
        //With these lines, I can read in the console that the right informations are loaded after a new login, but never appear in the menu, instead there are previous user's informations.
        console.log("Avatar de l'user :", $scope.userData.avatar);
        console.log("Nom de l'user :", $scope.userData.displayName);
        console.log("Email de l'user :", $scope.userData.email);
    }
}

// LOGIN FUNCTION in the controller

$scope.loginEmail = function($email, $password){
    
    var alertPopup;
    
    function signInSuccess(response) {
        console.log('signInSuccess : ', response);
        // This is to call a menu refresh, but dosen't work
        $scope.majDom();
        $state.go("menu.VNements");
    }
    
firebase.auth().signInWithEmailAndPassword($email, $password)
  .then(signInSuccess)
};


// MENU FUNCTION in the Menu Page of the creator

function ($scope, $stateParams, $state, $firebaseArray, $ionicHistory) {
    
//$ionicUser, $ionicAuth,

    var user = firebase.auth().currentUser;
    var uid;
    $scope.userData = user;
    
    $ionicHistory.clearCache();
    $ionicHistory.clearHistory();
    $ionicHistory.nextViewOptions({
        disableBack: true,
        historyRoot: true
    });
     
    if (user !== null) {
        $scope.userData.displayName = user.displayName;
        $scope.userData.email = user.email;
        $scope.userData.avatar = user.photoURL;
        uid = user.uid;
        console.log("Avatar de l'user :", $scope.userData.avatar);
        console.log("Nom de l'user :", $scope.userData.displayName);
        console.log("Email de l'user :", $scope.userData.email);
    }
In the side menu's HTML the Avatar is called like this :

{{userData.avatar}}


And the display name :

{{userData.displayName}}

Have you tried cache-view = "false" where you are declaring your side menu template?

Yep, on my side-menu I guess it is “unable view with back button”, I tried both but no change… : (

Please, I’m stuck… :’( Is anyone else have a thought ?

Solution :

I figured this out :), the solution was to initialize the current user inside a $scope.$on, because the problem is when you are creating an ionic side menu app, the menu is created once with the informations you gave him, to refresh it you need to have an event function like $scope.$on. Just have {{data.value}} in your HTML is not enough in this particular case.

// MAJ DOM (menu) FUNCTION in the controller
// function used for refresh the menu informations (avatar, displayname...)

$scope.majDom = function() 
{    
    var user;
    var uid;
    
     $scope.$on('$ionicView.enter', function() {
         
        user = firebase.auth().currentUser;
        $scope.userData = user;
    
        $scope.userData.displayName = user.displayName;
        $scope.userData.email = user.email;
        $scope.userData.avatar = user.photoURL;
        uid = user.uid;
        console.log("MENU : Avatar de l'user :", $scope.userData.avatar);
        console.log("MENU : Nom de l'user :", $scope.userData.displayName);
        console.log("MENU : Email de l'user :", $scope.userData.email);
    });
}
1 Like