@@ -8,6 +8,7 @@ import { LoggingService } from "../../../logging/logging.service";
88import { Entity } from "../../../entity/model/entity" ;
99import { User } from "../../../user/user" ;
1010import { ParsedJWT , parseJwt } from "../../../../session/session-utils" ;
11+ import { RemoteLoginNotAvailableError } from "./remote-login-not-available.error" ;
1112
1213/**
1314 * Handles the remote session with keycloak
@@ -33,8 +34,23 @@ export class KeycloakAuthService {
3334 */
3435 async login ( ) : Promise < SessionInfo > {
3536 if ( ! this . keycloakInitialised ) {
36- this . keycloakInitialised = true ;
37- const loggedIn = await this . keycloak . init ( {
37+ await this . initKeycloak ( ) ;
38+ }
39+
40+ await this . keycloak . updateToken ( ) ;
41+ let token = await this . keycloak . getToken ( ) ;
42+ if ( ! token ) {
43+ // Forward to the keycloak login page.
44+ await this . keycloak . login ( { redirectUri : location . href } ) ;
45+ token = await this . keycloak . getToken ( ) ;
46+ }
47+
48+ return this . processToken ( token ) ;
49+ }
50+
51+ private async initKeycloak ( ) {
52+ try {
53+ await this . keycloak . init ( {
3854 config : window . location . origin + "/assets/keycloak.json" ,
3955 initOptions : {
4056 onLoad : "check-sso" ,
@@ -44,28 +60,31 @@ export class KeycloakAuthService {
4460 // GitHub API rejects if non GitHub bearer token is present
4561 shouldAddToken : ( { url } ) => ! url . includes ( "api.github.com" ) ,
4662 } ) ;
47- if ( ! loggedIn ) {
48- // Forward to the keycloak login page.
49- await this . keycloak . login ( { redirectUri : location . href } ) ;
63+ } catch ( err ) {
64+ if (
65+ err ?. error ===
66+ "Timeout when waiting for 3rd party check iframe message."
67+ ) {
68+ // this is actually an expected scenario, user's internet is slow or not available
69+ err = new RemoteLoginNotAvailableError ( ) ;
70+ } else {
71+ this . logger . error ( "Keycloak init failed" , err ) ;
5072 }
5173
52- // auto-refresh expiring tokens, as suggested by https://github.com/mauriciovigolo/keycloak-angular?tab=readme-ov-file#keycloak-js-events
53- this . keycloak . keycloakEvents$ . subscribe ( ( event ) => {
54- if ( event . type == KeycloakEventType . OnTokenExpired ) {
55- this . login ( ) . catch ( ( err ) =>
56- this . logger . debug ( "automatic token refresh failed" , err ) ,
57- ) ;
58- }
59- } ) ;
74+ this . keycloakInitialised = false ;
75+ throw err ;
6076 }
6177
62- return this . keycloak
63- . updateToken ( )
64- . then ( ( ) => {
65- return this . keycloak . getToken ( ) ;
66- // TODO: should we notify the user to manually log in again when failing to refresh token?
67- } )
68- . then ( ( token ) => this . processToken ( token ) ) ;
78+ // auto-refresh expiring tokens, as suggested by https://github.com/mauriciovigolo/keycloak-angular?tab=readme-ov-file#keycloak-js-events
79+ this . keycloak . keycloakEvents$ . subscribe ( ( event ) => {
80+ if ( event . type == KeycloakEventType . OnTokenExpired ) {
81+ this . login ( ) . catch ( ( err ) =>
82+ this . logger . debug ( "automatic token refresh failed" , err ) ,
83+ ) ;
84+ }
85+ } ) ;
86+
87+ this . keycloakInitialised = true ;
6988 }
7089
7190 private processToken ( token : string ) : SessionInfo {
@@ -119,7 +138,7 @@ export class KeycloakAuthService {
119138 * Forward to the keycloak logout endpoint to clear the session.
120139 */
121140 async logout ( ) {
122- return this . keycloak . logout ( location . href ) ;
141+ return await this . keycloak . logout ( location . href ) ;
123142 }
124143
125144 /**
0 commit comments