@@ -429,15 +429,22 @@ class KeycloakService {
429429 let user = await this . userManager ! . getUser ( ) ;
430430
431431 if ( ! user ) {
432- // Try silent signin first
432+ // Try silent signin first with a timeout
433433 try {
434- user = await this . userManager ! . signinSilent ( ) ;
434+ console . log ( "Attempting silent signin..." ) ;
435+ user = await Promise . race ( [
436+ this . userManager ! . signinSilent ( ) ,
437+ new Promise < User | null > ( ( _ , reject ) =>
438+ setTimeout ( ( ) => reject ( new Error ( "Silent signin timeout" ) ) , 1000 )
439+ )
440+ ] ) ;
435441 } catch ( silentError ) {
436- console . log ( "Silent signin failed, checking if redirect callback" ) ;
442+ console . log ( "Silent signin failed:" , silentError . message ) ;
437443
438444 // Check if this is a callback from authentication
439445 if ( window . location . pathname . includes ( '/callback' ) ) {
440446 try {
447+ console . log ( "Handling authentication callback..." ) ;
441448 user = await this . userManager ! . signinRedirectCallback ( ) ;
442449 } catch ( callbackError ) {
443450 console . error ( "Signin redirect callback failed:" , callbackError ) ;
@@ -485,8 +492,14 @@ class KeycloakService {
485492 KeycloakService . refreshJwtToken ( ) ;
486493 callback ( true ) ;
487494 } else {
488- console . warn ( "Not authenticated!" ) ;
489- await this . login ( ) ;
495+ console . warn ( "Not authenticated! Initiating login..." ) ;
496+ // Don't await the login as it will redirect the page
497+ // The callback should be called with false to indicate auth is needed
498+ callback ( false ) ;
499+ // Start the login process without waiting
500+ this . login ( ) . catch ( error => {
501+ console . error ( "Login initiation failed:" , error ) ;
502+ } ) ;
490503 }
491504 } catch ( error ) {
492505 console . error ( "Failed to initialize OIDC Service" , error ) ;
@@ -591,6 +604,23 @@ class KeycloakService {
591604 return null ;
592605 }
593606 }
607+
608+ /**
609+ * Manually trigger login process
610+ * This is a public method that can be called when authentication is needed
611+ */
612+ public async triggerLogin ( ) : Promise < void > {
613+ console . log ( "Manually triggering login process" ) ;
614+ await this . login ( ) ;
615+ }
616+
617+ /**
618+ * Check if user needs authentication
619+ * Returns true if user needs to authenticate, false if already authenticated
620+ */
621+ public needsAuthentication ( ) : boolean {
622+ return ! this . isAuthenticated ( ) && ! this . isOfflineMode ;
623+ }
594624 }
595625
596626 export default KeycloakService ;
0 commit comments