Created
July 2, 2015 17:40
-
-
Save jdc18/91b57aa8d10251579ee7 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', ['ngRoute']); | |
app.controller('PageController', function($scope, $http, $timeout ,keycloakLauncher) { | |
$scope.loggedIn = keycloakLauncher.loggedIn; | |
$scope.user = function(){ | |
console.log("User keycloak ", keycloakLauncher.keycloak); | |
console.log("User loggedin ", keycloakLauncher.loggedIn); | |
if($scope.loggedIn){ | |
return keycloakLauncher.keycloak.idTokenParsed.email | |
} | |
//$scope.$apply(); | |
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(); | |
}; | |
}); | |
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, $routeProvider, keycloakLauncherProvider) { | |
keycloakLauncherProvider.keycloak = new Keycloak('WEB-INF/keycloak.json'); | |
keycloakLauncherProvider.logoutUrl = "https://localhost:8443/auth/realms/unika/tokens/logout?redirect_uri=http://unika.localdomain/index2.html"; | |
$httpProvider.interceptors.push('errorInterceptor'); | |
$httpProvider.interceptors.push('authInterceptor'); | |
$routeProvider.when('/', { | |
templateUrl: 'partials/products.htm', | |
resolve: { | |
init: ['initService', function(init) { | |
console.log("initservice xxxx",init.promise); | |
return init.promise; | |
}] | |
} | |
}); | |
}); | |
app.run(['keycloakLauncher','initService',function(keycloakLauncher,init) { | |
console.log("runner") | |
keycloakLauncher.keycloak.init({ onLoad: 'check-sso' }).success(function () { | |
//console.log("entro"); | |
keycloakLauncher.loggedIn = keycloakLauncher.keycloak.authenticated; | |
console.log("run login", keycloakLauncher.loggedIn); | |
init.defer.resolve(keycloakLauncher.keycloak); | |
}).error(function () { | |
window.location.reload(); | |
init.defer.reject('Failed to Init KC'); | |
}); | |
}]); | |
app.service('initService', ['$q', function ($q) { | |
var d = $q.defer(); | |
return { | |
defer: d, | |
promise: d.promise | |
}; | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment