I have gotten $state to work in one part of my application but not the other. It works the first time I call it like this: $state.go('main');
But I call it a little further down like this $state.go('signIn');
and can't get it to work there. What I want this code to do is if a user is not validated by $auth.validator
, I want it to redirect to the sign in page with a message that says "You need to sign in". How do I do this? Here is my code:
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ng-token-auth'])
.run(function($ionicPlatform, $location, $rootScope, $state) {
//I added in order to have redirect upon succesful login
$rootScope.$on('auth:login-success', function() {
console.log('success event triggered yo main');
// $location.path('main');
$state.go('main');
});
})
.config(function($stateProvider, $urlRouterProvider, $authProvider) {
.state('main', {
url: '/main',
templateUrl: 'templates/main.html',
resolve: {
auth: ['$auth', function($auth) {
console.log($auth.validateUser());
return $auth.validateUser().catch(function(res) {
console.log('in the validate user block');
$state.go('signIn');
});
}]
}
})
.state('home', {
url: '/home',
templateUrl: 'templates/home.html'
})
.state('signUp', {
url: '/signup',
templateUrl: 'templates/signup.html',
controller: 'UserCtrl'
})
.state('signIn', {
url: '/sign_in',
templateUrl: 'templates/new_user_session.html',
controller: 'UserCtrl'
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/home');
});
I get the folllowing error:
ionic.bundle.js:25642 ReferenceError: $state is not defined
at app.js:57
at processQueue (ionic.bundle.js:27879)
at ionic.bundle.js:27895
at Scope.$eval (ionic.bundle.js:29158)
at Scope.$digest (ionic.bundle.js:28969)
at Scope.$apply (ionic.bundle.js:29263)
at bootstrapApply (ionic.bundle.js:14945)
at Object.invoke (ionic.bundle.js:17762)
at doBootstrap (ionic.bundle.js:14943)
at bootstrap (ionic.bundle.js:14963)
UPDATE:
I have figured out half of the problem. I got $state to work by including it as a dependency:
auth: ['$auth', '$state', function($auth, $state) {
Now I just need to get the flash message to work and say "You need to sign in".