Handle Blob responses in AuthService.login and enhance JWT decoding logic. Add utility methods for Blob detection and conversion.

This commit is contained in:
akastijn 2025-07-06 20:53:03 +02:00
parent db394beda6
commit 4c38b070ea

View File

@ -1,6 +1,6 @@
import {Injectable} from '@angular/core';
import {LoginService} from '@api';
import {BehaviorSubject, Observable, throwError} from 'rxjs';
import {BehaviorSubject, from, Observable, of, switchMap, throwError} from 'rxjs';
import {catchError, tap} from 'rxjs/operators';
import {MatSnackBar} from '@angular/material/snack-bar';
import {JwtHelperService} from '@auth0/angular-jwt';
@ -25,16 +25,34 @@ export class AuthService {
this.checkAuthStatus();
}
private isBlob(value: any): boolean {
return value instanceof Blob;
}
private getAsBlob(value: any): Blob {
return value as Blob;
}
/**
* Attempt to login with the provided code
*/
public login(code: string): Observable<any> {
return this.loginService.login(code).pipe(
tap(jwt => {
console.log(jwt);
console.log(atob(jwt));
this.saveJwt(atob(jwt)); //Decode jwt from base64
this.isAuthenticatedSubject.next(true);
switchMap(response => {
if (this.isBlob(response)) {
return from(this.getAsBlob(response).text()).pipe(
tap(jwtText => {
console.log('JWT from Blob:', jwtText);
this.saveJwt(jwtText);
this.isAuthenticatedSubject.next(true);
})
);
} else {
console.log('JWT as string:', response);
this.saveJwt(atob(response));
this.isAuthenticatedSubject.next(true);
return of(response);
}
}),
catchError(error => {
this.snackBar.open('Login failed', '', {duration: 2000});