How to disable nav-bar controller


#1

I can hide nav-bar for login and registration page, however, I was wondering if there is a way to completely disable/ignore it? The problem I’m facing is that nav-bar has its own controller which among other functionalities checks if token exists. If it doesn’t, it automatically redirects to login page.

function checkIfTokenExists() {
                if (localStorage.getItem('token') == '') {
                    $state.go('login');
                }
            }

This of course works fine for every page except registration. I can’t get to registration page since the hidden nav-bar checks if the token exists, which of course doesn’t since user hasn’t registered yet. So I would like to somehow disable nav-bar for those two views or something.

I have also tried checking if the route is ‘signup’ like so:

function checkIfTokenExists() {
                if (localStorage.getItem('token') == '' && !($ionicHistory.currentStateName() == 'signup')) {
                    $state.go('login');
                }
            }

But that doesn’t work as expected…

Before I reformated that piece of code to be in navBarCtrl it was placed in beginning of every controller except login and sign up controllers, which was somehow bad and redundant programming so I wanted a more clean solution.

Any ideas?

EDIT: here is the complete controller if it helps:

.controller('navBarCtrl', ['Injection', '$scope', '$http', 'SERVER', '$ionicActionSheet', '$state',
        function (Injection, $scope, $http, SERVER, $ionicActionSheet, $state) {

            $scope.showActionsheet = function () {
                Injection.ActionSheetFactory.action($ionicActionSheet, $state);
            };

            $scope.$on('$locationChangeStart', function (event) {
                updateBasketCounter();
                checkIfTokenExists();
            });

            function updateBasketCounter() {
                Injection.ResourceFactory.getResource($http, SERVER, 'order/itemCount')
                    .then(function (response) {
                        $scope.item = {count: response.data.itemCount};
                        localStorage.setItem('countItem', response.data.itemCount);
                    });
            }

            function checkIfTokenExists() {
                if (localStorage.getItem('token') == '') {
                    $state.go('login');
                }
            }
        }])