Skip to content

Commit

Permalink
Merge pull request #778 from bcgov/oleks
Browse files Browse the repository at this point in the history
KeyCloak refresh token fix by Young Jin
  • Loading branch information
ychung-mot authored Nov 8, 2024
2 parents 163e129 + 4481f81 commit c31070b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions frontend/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ function initializeKeycloak(keycloak: KeycloakService) {
initOptions: {
onLoad: 'login-required',
pkceMethod: 'S256',
silentCheckSsoRedirectUri: window.location.origin + '/assets/silent-check-sso.html',
},
}).then(() => {
setupTokenRefresh(keycloak);
});
}

function setupTokenRefresh(keycloak: KeycloakService) {
const refreshInterval = 60 * 1000; // Check every 60 seconds

setInterval(async () => {
if (keycloak.isLoggedIn()) {
try {
const refreshed = await keycloak.updateToken(30); // Refresh if token will expire in 30 seconds
if (refreshed) {
console.log('Token refreshed successfully');
}
} catch (err) {
console.error('Failed to refresh token', err);
keycloak.logout();
}
}
}, refreshInterval);
}

0 comments on commit c31070b

Please sign in to comment.