1
1
import { Injectable } from '@angular/core' ;
2
- import { HttpClient , HttpHeaders , HttpErrorResponse } from '@angular/common/http' ;
2
+ import { HttpClient , HttpHeaders } from '@angular/common/http' ;
3
3
import { Observable , BehaviorSubject , catchError , of , tap } from 'rxjs' ;
4
4
5
5
declare global {
@@ -22,6 +22,10 @@ export class AuthService {
22
22
public readonly fcKeycloakClientScope : string ;
23
23
public readonly fcKeycloakClientId : string ;
24
24
public readonly fcKeycloakClientSecret : string ;
25
+ private demoUsername : string ;
26
+ private demoPassword : string ;
27
+ private loginRetryCount = 0 ;
28
+ private readonly maxLoginRetries = 1 ;
25
29
private usernameSubject : BehaviorSubject < string | null > = new BehaviorSubject < string | null > ( null ) ;
26
30
username$ : Observable < string | null > = this . usernameSubject . asObservable ( ) ;
27
31
private isLoggedInSubject : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( false ) ;
@@ -38,6 +42,8 @@ export class AuthService {
38
42
this . fcKeycloakClientScope = window . ENVIRONMENT ?. [ 'FC_KEYCLOAK_CLIENT_SCOPE' ] || 'gaia-x' ;
39
43
this . fcKeycloakClientId = window . ENVIRONMENT ?. [ 'FC_KEYCLOAK_CLIENT_ID' ] || 'federated-catalogue' ;
40
44
this . fcKeycloakClientSecret = window . ENVIRONMENT ?. [ 'FC_KEYCLOAK_CLIENT_SECRET' ] || 'keycloak-secret' ;
45
+ this . demoUsername = window . ENVIRONMENT ?. [ 'DEMO_USERNAME' ] || 'user' ;
46
+ this . demoPassword = window . ENVIRONMENT ?. [ 'DEMO_PASSWORD' ] || 'password' ;
41
47
const storedUsername = localStorage . getItem ( 'username' ) ;
42
48
const storedToken = localStorage . getItem ( 'accessToken' ) ;
43
49
const storedRefreshToken = localStorage . getItem ( 'refreshToken' ) ;
@@ -58,16 +64,7 @@ export class AuthService {
58
64
this . refreshAccessToken ( ) ;
59
65
}
60
66
} else {
61
- // Log in per env variables for demo purposes
62
- const demoUsername = window . ENVIRONMENT ?. [ 'DEMO_USERNAME' ] ;
63
- const demoPassword = window . ENVIRONMENT ?. [ 'DEMO_PASSWORD' ] ;
64
-
65
- if ( demoUsername && demoPassword ) {
66
- this . login ( demoUsername , demoPassword ) . subscribe ( ( response ) => {
67
- this . handleTokenResponse ( response ) ;
68
- this . isLoggedInSubject . next ( true ) ;
69
- } ) ;
70
- }
67
+ this . loginWithDemoCredentials ( ) ;
71
68
}
72
69
}
73
70
@@ -96,6 +93,19 @@ export class AuthService {
96
93
) ;
97
94
}
98
95
96
+ loginWithDemoCredentials ( ) : void {
97
+ if ( this . demoUsername && this . demoPassword ) {
98
+ this . login ( this . demoUsername , this . demoPassword ) . subscribe ( {
99
+ next : ( response ) => {
100
+ this . handleTokenResponse ( response ) ;
101
+ } ,
102
+ error : ( ) => {
103
+ this . logout ( ) ;
104
+ } ,
105
+ } ) ;
106
+ }
107
+ }
108
+
99
109
handleTokenResponse ( response : TokenResponse ) : void {
100
110
this . accessToken = response . access_token ;
101
111
this . refreshToken = response . refresh_token ;
@@ -132,10 +142,11 @@ export class AuthService {
132
142
this . http
133
143
. post < TokenResponse > ( this . fcKeycloakAuthUrl , body . toString ( ) , { headers } )
134
144
. pipe (
135
- tap ( ( response : TokenResponse ) => this . handleTokenResponse ( response ) ) ,
136
- catchError ( ( error : HttpErrorResponse ) => {
137
- console . error ( 'Failed to refresh token' , error ) ;
138
- this . logout ( ) ;
145
+ tap ( ( response ) => {
146
+ this . handleTokenResponse ( response ) ;
147
+ } ) ,
148
+ catchError ( ( ) => {
149
+ this . loginWithDemoCredentials ( ) ;
139
150
return of ( null ) ;
140
151
} ) ,
141
152
)
0 commit comments