Skip to content

Commit

Permalink
fix: retry login after expired refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
jxhnx committed Nov 6, 2024
1 parent 4c0720e commit 2976309
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 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 = window.ENVIRONMENT?.['DEMO_USERNAME'];
private demoPassword = window.ENVIRONMENT?.['DEMO_PASSWORD'];
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 Down Expand Up @@ -58,12 +62,8 @@ 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) => {
if (this.demoUsername && this.demoPassword) {
this.login(this.demoUsername, this.demoPassword).subscribe((response) => {
this.handleTokenResponse(response);
this.isLoggedInSubject.next(true);
});
Expand Down Expand Up @@ -97,6 +97,7 @@ export class AuthService {
}

handleTokenResponse(response: TokenResponse): void {
this.loginRetryCount = 0;
this.accessToken = response.access_token;
this.refreshToken = response.refresh_token;
this.expiresIn = response.expires_in;
Expand Down Expand Up @@ -132,17 +133,28 @@ 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();
catchError(() => {
if (this.demoUsername && this.demoPassword && this.loginRetryCount < this.maxLoginRetries) {
this.loginRetryCount++;
this.login(this.demoUsername, this.demoPassword).subscribe({
next: (response) => {
this.handleTokenResponse(response);
},
error: () => {
this.logout();
},
});
} else {
this.logout();
}
return of(null);
}),
)
.subscribe();
}

logout(): void {
this.loginRetryCount = 0;
this.accessToken = '';
this.refreshToken = '';
this.expiresIn = 0;
Expand Down

0 comments on commit 2976309

Please sign in to comment.