Skip to content

Commit 79ae79f

Browse files
committed
fix: login after expired refresh token
1 parent 4c0720e commit 79ae79f

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/app/services/auth.service.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
2+
import { HttpClient, HttpHeaders } from '@angular/common/http';
33
import { Observable, BehaviorSubject, catchError, of, tap } from 'rxjs';
44

55
declare global {
@@ -22,6 +22,10 @@ export class AuthService {
2222
public readonly fcKeycloakClientScope: string;
2323
public readonly fcKeycloakClientId: string;
2424
public readonly fcKeycloakClientSecret: string;
25+
private demoUsername: string;
26+
private demoPassword: string;
27+
private loginRetryCount = 0;
28+
private readonly maxLoginRetries = 1;
2529
private usernameSubject: BehaviorSubject<string | null> = new BehaviorSubject<string | null>(null);
2630
username$: Observable<string | null> = this.usernameSubject.asObservable();
2731
private isLoggedInSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
@@ -38,6 +42,8 @@ export class AuthService {
3842
this.fcKeycloakClientScope = window.ENVIRONMENT?.['FC_KEYCLOAK_CLIENT_SCOPE'] || 'gaia-x';
3943
this.fcKeycloakClientId = window.ENVIRONMENT?.['FC_KEYCLOAK_CLIENT_ID'] || 'federated-catalogue';
4044
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';
4147
const storedUsername = localStorage.getItem('username');
4248
const storedToken = localStorage.getItem('accessToken');
4349
const storedRefreshToken = localStorage.getItem('refreshToken');
@@ -58,16 +64,7 @@ export class AuthService {
5864
this.refreshAccessToken();
5965
}
6066
} 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();
7168
}
7269
}
7370

@@ -96,6 +93,19 @@ export class AuthService {
9693
);
9794
}
9895

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+
99109
handleTokenResponse(response: TokenResponse): void {
100110
this.accessToken = response.access_token;
101111
this.refreshToken = response.refresh_token;
@@ -132,10 +142,11 @@ export class AuthService {
132142
this.http
133143
.post<TokenResponse>(this.fcKeycloakAuthUrl, body.toString(), { headers })
134144
.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();
139150
return of(null);
140151
}),
141152
)

0 commit comments

Comments
 (0)