Skip to content

Commit bf4a9e4

Browse files
authored
Merge pull request #771 from bcgov/rsbc/7.0.0
Rsbc/7.0.0 fix authentication loop
2 parents 59c4cb9 + 7276d13 commit bf4a9e4

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

forms-flow-service/src/keycloak/KeycloakService.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,12 @@ class KeycloakService {
413413

414414
// Check if offline and try to initialize with stored credentials
415415
if (!navigator.onLine) {
416-
console.log("Offline detected during initialization");
416+
console.log("[KeycloakService] - Offline detected during initialization");
417417
if (this.initOfflineMode()) {
418418
callback(true);
419419
return;
420420
} else {
421-
console.warn("No valid offline credentials found");
421+
console.warn("[KeycloakService] - No valid offline credentials found");
422422
callback(false);
423423
return;
424424
}
@@ -428,26 +428,28 @@ class KeycloakService {
428428
// Check if user is already authenticated
429429
let user = await this.userManager!.getUser();
430430

431-
if (!user) {
431+
console.log("[KeycloakService] - User: ", user);
432+
console.log("[KeycloakService] - Is expired: ", user?.expired);
433+
if (!user || user.expired) {
432434
// Try silent signin first with a timeout
433435
try {
434-
console.log("Attempting silent signin...");
436+
console.log("[KeycloakService] - Attempting silent signin...");
435437
user = await Promise.race([
436438
this.userManager!.signinSilent(),
437439
new Promise<User | null>((_, reject) =>
438440
setTimeout(() => reject(new Error("Silent signin timeout")), 2000)
439441
)
440442
]);
441443
} catch (silentError) {
442-
console.log("Silent signin failed:", silentError.message);
444+
console.log("[KeycloakService] - Silent signin failed:", silentError.message);
443445

444446
// Check if this is a callback from authentication
445447
if (window.location.pathname.includes('/callback')) {
446448
try {
447-
console.log("Handling authentication callback...");
449+
console.log("[KeycloakService] - Handling authentication callback...");
448450
user = await this.userManager!.signinCallback();
449451
} catch (callbackError) {
450-
console.error("Signin redirect callback failed:", callbackError);
452+
console.error("[KeycloakService] - Signin redirect callback failed:", callbackError);
451453
callback(false);
452454
return;
453455
}
@@ -457,12 +459,12 @@ class KeycloakService {
457459

458460
if (user && !user.expired) {
459461
this.isInitialized = true;
460-
console.log("Authenticated");
462+
console.log("[KeycloakService] - Authenticated");
461463

462464
// Extract user roles from token claims
463465
const userRoles = this.extractUserRoles(user);
464466
if (!userRoles || userRoles.length === 0) {
465-
console.warn("No user roles found");
467+
console.warn("[KeycloakService] - No user roles found");
466468
callback(false);
467469
return;
468470
}
@@ -485,20 +487,18 @@ class KeycloakService {
485487
{ once: false }
486488
);
487489
} else {
488-
console.info("Init OIDC - not storing the refresh token.");
490+
console.info("[KeycloakService] - Init OIDC - not storing the refresh token.");
489491
}
490492

491493
this.refreshToken();
492494
KeycloakService.refreshJwtToken();
493495
callback(true);
494496
} else {
495-
console.warn("Not authenticated! Initiating login...");
496-
this.login().catch(error => {
497-
console.error("Login initiation failed:", error);
498-
});
497+
console.warn("[KeycloakService] - Not authenticated! Initiating login...");
498+
this.login();
499499
}
500500
} catch (error) {
501-
console.error("Failed to initialize OIDC Service", error);
501+
console.error("[KeycloakService] - Failed to initialize OIDC Service", error);
502502
callback(false);
503503
}
504504
}
@@ -589,7 +589,8 @@ class KeycloakService {
589589
*/
590590
public async handleCallback(): Promise<User | null> {
591591
try {
592-
const user = await this.userManager!.signinRedirectCallback();
592+
console.log("Handling signin callback...");
593+
const user = await this.userManager!.signinCallback();
593594
if (user) {
594595
this.handleUserLoaded(user);
595596
this.isInitialized = true;

0 commit comments

Comments
 (0)