I am getting a login page finished my first time using Ionic Mobile Framework and having just freshly learned Angular JS. When the login page starts up I want to move to the homepage only if the session has not expired. Otherwise we want to go to the login page.It works great going back and forth between the logout "songs" screen/state and login screen/state however I have one problem.
When the app boots up it goes to the songs state or "home" page, however it doesn't run the controller code for that state. In the controller code I hit a route on our website to see if the session is still active. If so it will return JSON, otherwise my authInterceptor will takeover and transition the state to login.
The bottom line is the controller is not running on app startup and I can't find out why no matter how much googling I do. Below is some code example.
.run(function($ionicPlatform, $cordovaSplashscreen, localstorage, $state) {
setTimeout(function() {
$cordovaSplashscreen.hide()
}, 1500)
$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();
}
if(localstorage.get('subdomain') == undefined ||
localstorage.get('subdomain') == 'null' ||
localstorage.get('subdomain') == null ||
localstorage.get('subdomain') == 'undefined' ||
localstorage.get('subdomain') == "")
{
$state.go('subdomain');
}
else
$state.go('songs');
});
})
.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
$urlRouterProvider.otherwise("/songs");
$stateProvider
.state('login', {
url: "/login",
templateUrl: "templates/login.html",
controller: 'LoginCtrl'
})
.state('subdomain', {
url: "/subdomain",
templateUrl: "templates/subdomain.html",
controller: 'SubdomainCtrl'
})
.state('songs', {
url: "/songs",
templateUrl: "templates/songs.html",
controller: 'SongsCtrl'
})
$httpProvider.interceptors.push('authInterceptor');
});
// register the interceptor as a service
.factory('authInterceptor', function($q, $injector) {
return {
// optional method
'responseError': function(rejection) {
if(rejection.status == 0 || rejection.status == 403){
$injector.get('$state').transitionTo('login',null,{ reload: true, inherit: true, notify: true });
if(rejection.config.url.indexOf('login') > -1){
navigator.notification.alert("Invalid username or password.", function(){}, "Login Failed");
}
}
return $q.reject(rejection);
}
};
})
.controller('SongsCtrl', function($scope, $state, user) {
user.check(); //Resource that hits route on server
$scope.logout = function()
{
user.logout();
$state.go('login');
}
})
Thanks so much. James
The issue was a combination of an incorrect server response that the interceptor wasn't able to handle as well as a bad debugging tool which wouldn't let me see if the controller had fired on startup.