Skip to content

Commit

Permalink
fix: login after expired refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
jxhnx committed Nov 7, 2024
1 parent 4c0720e commit 79ae79f
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, BehaviorSubject, catchError, of, tap } from 'rxjs';

declare global {
Expand All @@ -22,6 +22,10 @@ export class AuthService {
public readonly fcKeycloakClientScope: string;
public readonly fcKeycloakClientId: string;
public readonly fcKeycloakClientSecret: string;
private demoUsername: string;
private demoPassword: string;
private loginRetryCount = 0;
private readonly maxLoginRetries = 1;
private usernameSubject: BehaviorSubject<string | null> = new BehaviorSubject<string | null>(null);
username$: Observable<string | null> = this.usernameSubject.asObservable();
private isLoggedInSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
Expand All @@ -38,6 +42,8 @@ export class AuthService {
this.fcKeycloakClientScope = window.ENVIRONMENT?.['FC_KEYCLOAK_CLIENT_SCOPE'] || 'gaia-x';
this.fcKeycloakClientId = window.ENVIRONMENT?.['FC_KEYCLOAK_CLIENT_ID'] || 'federated-catalogue';
this.fcKeycloakClientSecret = window.ENVIRONMENT?.['FC_KEYCLOAK_CLIENT_SECRET'] || 'keycloak-secret';
this.demoUsername = window.ENVIRONMENT?.['DEMO_USERNAME'] || 'user';
this.demoPassword = window.ENVIRONMENT?.['DEMO_PASSWORD'] || 'password';
const storedUsername = localStorage.getItem('username');
const storedToken = localStorage.getItem('accessToken');
const storedRefreshToken = localStorage.getItem('refreshToken');
Expand All @@ -58,16 +64,7 @@ export class AuthService {
this.refreshAccessToken();
}
} else {
// Log in per env variables for demo purposes
const demoUsername = window.ENVIRONMENT?.['DEMO_USERNAME'];
const demoPassword = window.ENVIRONMENT?.['DEMO_PASSWORD'];

if (demoUsername && demoPassword) {
this.login(demoUsername, demoPassword).subscribe((response) => {
this.handleTokenResponse(response);
this.isLoggedInSubject.next(true);
});
}
this.loginWithDemoCredentials();
}
}

Expand Down Expand Up @@ -96,6 +93,19 @@ export class AuthService {
);
}

loginWithDemoCredentials(): void {
if (this.demoUsername && this.demoPassword) {
this.login(this.demoUsername, this.demoPassword).subscribe({
next: (response) => {
this.handleTokenResponse(response);
},
error: () => {
this.logout();
},
});
}
}

handleTokenResponse(response: TokenResponse): void {
this.accessToken = response.access_token;
this.refreshToken = response.refresh_token;
Expand Down Expand Up @@ -132,10 +142,11 @@ export class AuthService {
this.http
.post<TokenResponse>(this.fcKeycloakAuthUrl, body.toString(), { headers })
.pipe(
tap((response: TokenResponse) => this.handleTokenResponse(response)),
catchError((error: HttpErrorResponse) => {
console.error('Failed to refresh token', error);
this.logout();
tap((response) => {
this.handleTokenResponse(response);
}),
catchError(() => {
this.loginWithDemoCredentials();
return of(null);
}),
)
Expand Down

0 comments on commit 79ae79f

Please sign in to comment.