I'm importing the keycloak lib into angularjs, and after execute with success 'init method' the "service.keycloak" object is updated with properties that i wanted to use (as example: token). When calling my service in another controller the object "service.keycloak" is not loaded with properties like in init method . How can I do this?
My service factory:
(function TBayKeycloak(angular) {
"use strict";
var app = angular.module('TBayKeycloak',[]);
app.factory('KeycloakService', ['$window',
function KeycloakService($window,) {
var service = {};
service.init = function init() {
service.keycloak = new Keycloak({
url: 'http://localhost:8200/auth',
realm: 'master',
clientId: 'bpms-dev'
});
service.keycloak.init({
onLoad: 'login-required'
}).then(function (auth){
if(auth){
//Here my object "service.keycloak" load properties with success
$window.location = window.location.origin + '/main.html';
}else{
console.log("authentication failed");
}
}).catch(function(error) {
console.log('failed to initialize');
});
}
service.getKeycloakInstance = function getKeycloakInstance(){
//But here the object never loaded properties
return service.keycloak;
}
service.getToken = function getToken(){
if(service.keycloak){
return service.keycloak.token;
}
}
return service;
}]);
})(angular);
And Controller with call method from service:
app.controller('TitleCtrl', ['$scope', '$rootScope', '$location', 'CoreRes','KeycloakService',
function ($scope, $rootScope, $location, CoreRes, KeycloakService) {
//Here returns the object incomplete
var keycloak = KeycloakService.getKeycloakInstance();
}
]);