I want to evaluate the user when the app start if it's authenticated change the state to "categories" if not go to "land" state. i reached this using $stateChangeStart
in the run
block like so :
.run(function($ionicPlatform, DSCacheFactory, $rootScope, $auth, $state) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
$rootScope.$on('$stateChangeStart', function (event, toState) {
if (!$auth.isAuthenticated() && toState.name !== 'land') {
event.preventDefault();
$state.go('land');
}
});
})
and like this if it's authenticated:
$urlRouterProvider.otherwise('app/categories');
every thing works fine user get evaluated correctly but, clicking on anything in the app taking me to the land
state. Any idea why!
The router code :
.config(function($stateProvider, $urlRouterProvider, $authProvider, $ionicAppProvider, API_URL) {
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl'
})
.state('land', {
url: '/land',
templateUrl: 'templates/land.html',
controller: 'LandCtrl'
})
.state('signup', {
url: '/sign-up',
templateUrl: 'templates/sign-up.html',
controller: 'SignUpCtrl'
})
.state('signin', {
url: '/sign-in',
templateUrl: 'templates/sign-in.html',
controller: 'SignInCtrl'
})
.state('app.categories', {
url: '/categories',
views: {
'menuContent': {
templateUrl: 'templates/categories.html',
controller: 'categoriesCtrl',
}
}
});