Created
June 26, 2015 18:24
-
-
Save jdc18/1ecc5357b68baf447919 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.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',function(keycloakLauncher) { | |
console.log("runner") | |
keycloakLauncher.keycloak.init({ onLoad: 'check-sso' }).success(function () { | |
console.log("entor"); | |
keycloakLauncher.loggedIn = true; | |
console.log("lgin", keycloakLauncher.loggedIn) | |
}).error(function () { | |
window.location.reload(); | |
}); | |
}]); | |
app.controller('PageController', function($scope, $http ,keycloakLauncher) { | |
$scope.loggedIn = keycloakLauncher.loggedIn; | |
$scope.user = function(){ | |
console.log("que paso") | |
if($scope.loggedIn){ | |
return keycloak.idTokenParsed.email | |
} | |
return "Anonymous"; | |
} | |
$scope.products = []; | |
$scope.reloadData = function() { | |
$http.get("https://localhost:8080/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; | |
window.location = "/"; | |
}; | |
$scope.login = function () { | |
console.log('*** LOGIN'); | |
keycloakLauncher.keycloak.login(); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment