Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created March 9, 2014 06:11
Show Gist options
  • Save kentcdodds/9443522 to your computer and use it in GitHub Desktop.
Save kentcdodds/9443522 to your computer and use it in GitHub Desktop.
Angular-UI Router states auth/anon
$stateProvider.
state('main', {
abstract: true,
url: '/',
templateUrl: '/main/index.html',
controller: 'SuperCtrl',
resolve: {
isAuthenticated: function($q, $http) {
var deferred = $q.defer();
$http.get('/api/v1/auth/isAuthenticated').then(function(response) {
deferred.resolve(response.data.isAuthenticated);
}, deferred.reject);
return deferred.promise;
}
},
onEnter: function($state, isAuthenticated) {
$state.go('main.' + (isAuthenticated ? 'auth' : 'anon'));
}
}).
state('main.anon', {
url: '',
templateUrl: '/main/anon/index.html',
controller: 'FrontPageCtrl'
}).
state('main.auth', {
url: '',
templateUrl: '/main/index.html',
controller: 'MainCtrl'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment