I am using a plugin "keycloak-angular".
Version :
"keycloak-angular": "8.0.1",
"keycloak-js": "11.0.2",
angular : 9.1.12
I have set "Access token lifespan" to 1 minute. And I am trying to update Tokens when access token is expired by checking with Keycloak.isTokenExpired(). Its working but the issue that I am facing is, sometimes this function gets called and most of the time its not getting called. What I want to achieve -> Whenever the access token expires, I want to update the token, keeping the user logged-In. Tried to find this issue over the internet but couldn't find anything similar.
import {KeycloakService} from 'keycloak-angular';
export function initializer(keycloak: KeycloakService) {
return () => {
return new Promise(async (resolve, reject) => {
try {
const _REALM = "realm";
const _URL = "http://example.com";
const _CLIENT_ID = "id"
await keycloak.init({
config: {
realm: _REALM,
url: _URL,
clientId: _CLIENT_ID,
},
initOptions: {
onLoad: 'login-required',
checkLoginIframe: false
},
enableBearerInterceptor: true,
bearerExcludedUrls: ['/assets', '/clients/public']
})
const keycloakAuth = keycloak.getKeycloakInstance();
const updateToken = async (): Promise < string > => {
const {success,error} = keycloakAuth.updateToken(5);
return new Promise < string > ((res, rej) => {
success(() => res(keycloakAuth.token));
error(rej);
});
}
const login = async (): Promise < void > => {
const {success,error} = keycloakAuth.login();
return new Promise < void > ((res2, rej2) => {
success(res2);
error(rej2);
});
}
keycloakAuth.onTokenExpired = () => {
if (keycloakAuth.refreshToken) {
updateToken();
} else {
login();
}
}
resolve();
} catch (error) {
reject(error);
}
});
}; }
This is the code I am using, its placed in app-init.ts.
getting this from network tab -
expires_in: 60
refresh_expires_in: 1800
Screenshot of provider in main module file. Provider in main module
PS- New to this, apologies if there is a mistake in question or if i left something