Last active
January 27, 2016 14:42
-
-
Save jdc18/7b8ca0c4bf8a4cdbfdbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var app = angular.module('myApp', []); | |
app.controller('PageController', function($rootScope,$scope, $http ,keycloakLauncher) { | |
$scope.loggedIn = keycloakLauncher.loggedIn; | |
$scope.user = function(){ | |
console.log("keycloak ", keycloakLauncher.keycloak); | |
console.log("loggedin ", keycloakLauncher.loggedIn); | |
if($scope.loggedIn){ | |
/*if(keycloakLauncher.keycloak.idTokenParsed == undefined){ | |
return "Anonymous" | |
}*/ | |
return keycloakLauncher.keycloak.idTokenParsed.email | |
} | |
return "Anonymous"; | |
}; | |
$scope.products = []; | |
$scope.reloadData = function() { | |
$http.get("https://localhost:8443/unika/usuarios").success(function(data) { | |
$scope.products = angular.fromJson(data); | |
}); | |
}; | |
$scope.logout = function(){ | |
console.log('*** LOGOUT'); | |
keycloakLauncher.loggedIn = false; | |
keycloakLauncher.keycloak = null; | |
window.location = keycloakLauncher.logoutUrl; | |
}; | |
$scope.login = function () { | |
console.log('*** LOGIN'); | |
keycloakLauncher.keycloak.login(); | |
}; | |
$rootScope.watch('loggedIn', function(){ | |
$scope.loggedIn = keycloakLauncher.loggedIn; | |
$scope.user(); | |
$scope.$apply(); | |
}); | |
}); | |
app.provider('keycloakLauncher', function keycloakLauncherProvider() { | |
this.keycloak = {}; | |
this.loggedIn = false; | |
this.logoutUrl = "/"; | |
this.$get = function () { | |
var keycloak = this.keycloak; | |
var loggedIn = this.loggedIn; | |
//var logoutUrl | |
return { | |
keycloak: keycloak, | |
loggedIn: loggedIn, | |
logoutUrl: this.logoutUrl | |
}; | |
}; | |
}); | |
app.factory('authInterceptor', function($q, keycloakLauncher) { | |
return { | |
request: function (config) { | |
var deferred = $q.defer(); | |
var keycloak = keycloakLauncher.keycloak; | |
console.log("authint", keycloakLauncher.loggedIn); | |
if (keycloak.token) { | |
keycloak.updateToken(5).success(function() { | |
config.headers = config.headers || {}; | |
config.headers.Authorization = 'Bearer ' + keycloak.token; | |
deferred.resolve(config); | |
}).error(function() { | |
deferred.reject('Failed to refresh token'); | |
}); | |
} | |
return deferred.promise; | |
} | |
}; | |
}); | |
app.factory('errorInterceptor', function($q) { | |
return function(promise) { | |
return promise.then(function(response) { | |
return response; | |
}, function(response) { | |
if (response.status == 401) { | |
console.log('session timeout?'); | |
logout(); | |
} else if (response.status == 403) { | |
alert("Forbidden"); | |
} else if (response.status == 404) { | |
alert("Not found"); | |
} else if (response.status) { | |
if (response.data && response.data.errorMessage) { | |
alert(response.data.errorMessage); | |
} else { | |
alert("An unexpected server error has occurred"); | |
} | |
} | |
return $q.reject(response); | |
}); | |
}; | |
}); | |
app.config(function($httpProvider, keycloakLauncherProvider) { | |
keycloakLauncherProvider.keycloak = new Keycloak('WEB-INF/keycloak.json'); | |
//keycloakLauncherProvider.logoutUrl = keycloakAuth.authServerUrl + "/realms/demo/tokens/logout?redirect_uri=/angular-product/index.html"; | |
$httpProvider.interceptors.push('errorInterceptor'); | |
$httpProvider.interceptors.push('authInterceptor'); | |
}); | |
app.run(['keycloakLauncher','$rootScope',function(keycloakLauncher,$rootScope) { | |
console.log("runner") | |
$rootScope.loggedIn = false; | |
keycloakLauncher.keycloak.init({ onLoad: 'check-sso' }).success(function () { | |
console.log("entro"); | |
keycloakLauncher.loggedIn = keycloakLauncher.keycloak.authenticated; | |
console.log("login", keycloakLauncher.loggedIn); | |
$rootScope.loggedIn = true; | |
}).error(function () { | |
window.location.reload(); | |
}); | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I saw your post on the Keycloak mailing list and was hoping to get some feedback. For integrating this with Angular 1.4 where
responseInterceptors
is now deprecated, I didn't create a separate interceptor,errorInterceptor
, for the error handling. I just moved the error handling into the 'responseError' section of theauthInterceptor
( See SO post). It seems like having the separate error interceptor does not work as you may have intended because it would not catch any bad responses. Did you end up going with this implementation?