I am currently working with keycloak-js version 8.0.1 and have a function called getToken that checks if the token is expired. If it is expired, the function refreshes it; otherwise, it returns the current token. The issue I am facing is that even though the warning message for an expired token is displayed, the token itself is not being updated.
TokenService
@Injectable()
export class TokenService {
static auth: any = {};
constructor() { }
static init(): Promise<any> {
const keycloakAuth: Keycloak.KeycloakInstance = Keycloak({
url: config.keycloak.url,
realm: config.keycloak.realm,
clientId: config.keycloak.clientId
});
return new Promise((resolve, reject) => {
keycloakAuth.init({ onLoad: 'check-sso', flow: 'implicit', checkLoginIframe: false }).success(() => {
TokenService.auth.authz = keycloakAuth;
resolve();
}).error(() => {
reject();
});
});
}
getToken(): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (TokenService.auth.authz.isTokenExpired()) {
console.warn("Token expired !");
TokenService.auth.authz.updateToken(1800).success(() => {
console.log("Token updated");
resolve(<string>TokenService.auth.authz.token);
}).error(() => {
reject('Failed to refresh token');
});
} else {
resolve(<string>TokenService.auth.authz.token);
}
});
}
}