diff --git a/frontend/src/app/services/auth.service.ts b/frontend/src/app/services/auth.service.ts index 53883e6..ac10c9b 100644 --- a/frontend/src/app/services/auth.service.ts +++ b/frontend/src/app/services/auth.service.ts @@ -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 { 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});