Hi,
I have an app based on the sidemenu example.
I listen to $stateChangeStart to listen for state change and enforce login.
It works good beside the application start phase.
When I open the app the default route: app.services is selected but $stateChangeStart doesn’t fire and so the view is shown even if the user is not logged in.
Is it normal that $stateChangeStart dosen’t fire at startup? Is there any other event to listen to at startup?
I’ using Ionic 1.0 rc4 and down here my code in app.js.
Thank you for any advice!
app.js:
angular.module('starter', ['ionic', 'starter.controllers'])
.run(function($ionicPlatform, $rootScope, $state, AuthService) {
$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, toParams, fromState, fromParams){
console.log("fires at startup")
if (toState.authenticate && !AuthService.isAuthenticated()){
// User isn’t authenticated
$state.go("app.login");
event.preventDefault();
}
});
});
})
.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider, $resourceProvider, $httpProvider) {
$ionicConfigProvider.backButton.text('').icon('ion-ios7-arrow-left');
$resourceProvider.defaults.stripTrailingSlashes = false;
$stateProvider
.state('app', {
url: "/app",
abstract: true,
templateUrl: "templates/menu.html",
controller: 'AppCtrl',
authenticate: true
})
.state('app.login', {
url: "/login",
views: {
'menuContent': {
templateUrl: "templates/login.html"
}
},
authenticate: false
})
.state('app.services', {
cache: false,
url: "/services",
views: {
'menuContent': {
templateUrl: "templates/services.html",
controller: 'ServicesCtrl'
}
},
authenticate: true
})
.state('app.service', {
cache: false,
url: "/services/:serviceId",
views: {
'menuContent': {
templateUrl: "templates/service.html",
controller: 'ServiceCtrl'
}
},
authenticate: true
})
.state('app.issue', {
cache: false,
url: "/issue/:issueId",
views: {
'menuContent': {
templateUrl: "templates/issue.html",
controller: 'IssueCtrl'
}
},
authenticate: true
})
.state('app.update', {
cache: false,
url: "/update/:updateId",
views: {
'menuContent': {
templateUrl: "templates/update.html",
controller: 'UpdateCtrl'
}
},
authenticate: true
})
.state('app.updateadd', {
url: "/issue/:issueId/updateadd",
views: {
'menuContent': {
templateUrl: "templates/updateadd.html",
controller: 'UpdateAddCtrl'
}
},
authenticate: true
})
.state('app.updateedit', {
url: "/update/:updateId/updateedit",
views: {
'menuContent': {
templateUrl: "templates/updateedit.html",
controller: 'UpdateEditCtrl'
}
},
authenticate: true
})
.state('app.issueadd', {
url: "/service/:serviceId/issueadd",
views: {
'menuContent': {
templateUrl: "templates/issueadd.html",
controller: 'IssueAddCtrl'
}
},
authenticate: true
})
.state('app.issueedit', {
url: "/issue/:issueId/issueedit",
views: {
'menuContent': {
templateUrl: "templates/issueedit.html",
controller: 'IssueEditCtrl'
}
},
authenticate: true
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/services');
});