Skip to content

Instantly share code, notes, and snippets.

@robwormald
Forked from sonicparke/JWT.js
Last active Aug 29, 2015
Embed
What would you like to do?
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){
console.log('applist resolve fired');
//console.log('$state', $state);
//var allowed = JWT.getToken();
$state.go('login');
//if(allowed === null){
// //console.log('allowed', allowed);
// $state.go('login');
//}
}]
}
})
.state('login', {
url: '/login',
templateUrl: 'app/partials/login.html',
controller: 'Login',
controllerAs: "vm"
});
$urlRouterProvider.otherwise('applist');
$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();
// deferred.resolve(store.getItem(key));
// return deferred.promise;
//};
function getToken() {
console.log('store.getItem(key)', store.getItem(key));
var token = store.getItem(key);
if(token){
return $q.when(token);
}
return $q.reject('NO_TOKEN_FOUND');
};
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