Skip to content

Instantly share code, notes, and snippets.

@sonicparke
Last active Aug 29, 2015
Embed
What would you like to do?
ui-router $state.go() finally working
app.config(['$stateProvider', '$provide', '$httpProvider', '$tooltipProvider', '$urlRouterProvider', '$locationProvider', function ($stateProvider, $provide, $httpProvider, $tooltipProvider, $urlRouterProvider, $locationProvider){
//$locationProvider.html5Mode(true);
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.interceptors.push('AuthInterceptor');
$stateProvider
.state('applist', {
url: '/applist',
templateUrl: 'app/partials/appList.html',
controller: 'AppList',
controllerAs: "vm",
authenticate: true,
resolve: {
validate: ['JWT', '$state', function(JWT, $state){
var authenticated;
return JWT.getToken().then(function(res){
authenticated = res;
if(!authenticated){
$state.go('login');
}
});
}]
}
})
.state('login', {
url: '/login',
templateUrl: 'app/partials/login.html',
controller: 'Login',
controllerAs: "vm",
resolve: {
validate: ['JWT', '$state', function(JWT, $state){
var authenticated;
return JWT.getToken().then(function(res){
authenticated = res;
if(authenticated){
$state.go('applist');
}
});
}]
}
});
$urlRouterProvider.otherwise('login');
$tooltipProvider.options( {appendToBody: true} );
}]);
(function(){
app.factory('JWT', JWT);
function JWT($window, $q){
var store = $window.localStorage;
var key = 'AXC_API_Storage.Token';
var service = {
getToken: getToken,
setToken: setToken
};
return service;
function getToken() {
var deferred = $q.defer();
var token = store.getItem(key);
deferred.resolve(token);
return deferred.promise;
};
function setToken(token) {
if(token) {
store.setItem(key, token);
console.log('token', token);
}
else {
store.removeItem(key);
console.log('no token works!');
}
};
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment