From c77ea3822e750350a4c14cc14ff9dc0d4d49640a Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Fri, 31 May 2024 15:24:53 -0400 Subject: [PATCH] fix(auth): render username in top bar --- src/app/Shared/Services/Login.service.tsx | 21 ++++++++++++++++++- src/mirage/index.ts | 2 +- .../Shared/Services/Login.service.test.tsx | 10 +++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/app/Shared/Services/Login.service.tsx b/src/app/Shared/Services/Login.service.tsx index 3007f8ebe..23fd5bf4c 100644 --- a/src/app/Shared/Services/Login.service.tsx +++ b/src/app/Shared/Services/Login.service.tsx @@ -27,8 +27,27 @@ export class LoginService { constructor(private readonly settings: SettingsService) { this.authority = process.env.CRYOSTAT_AUTHORITY || '.'; - this.username.next('' /*TODO get this from X-Forwarded headers: this.getCacheItem(this.USER_KEY)*/); this.sessionState.next(SessionState.CREATING_USER_SESSION); + + fromFetch(`${this.authority}/api/v2.1/auth`, { + credentials: 'include', + mode: 'cors', + method: 'POST', + body: null, + }) + .pipe( + concatMap((response) => { + let gapAuth = response?.headers?.get('Gap-Auth'); + if (gapAuth) { + return new Promise((r) => r({ data: { result: { username: gapAuth } } } as any)); + } + return response.json(); + }), + catchError(() => of({ data: { result: { username: '' } } } as any)), + ) + .subscribe((v) => { + this.username.next(v?.data?.result?.username ?? ''); + }); } getUsername(): Observable { diff --git a/src/mirage/index.ts b/src/mirage/index.ts index 20370ccb8..61f75db4f 100644 --- a/src/mirage/index.ts +++ b/src/mirage/index.ts @@ -78,7 +78,7 @@ export const startMirage = ({ environment = 'development' } = {}) => { this.post('api/v2.1/auth', () => { return new Response( 200, - { 'X-WWW-Authenticate': 'None' }, + {}, { meta: { status: 'OK', diff --git a/src/test/Shared/Services/Login.service.test.tsx b/src/test/Shared/Services/Login.service.test.tsx index 8c331ca68..f7c6e565a 100644 --- a/src/test/Shared/Services/Login.service.test.tsx +++ b/src/test/Shared/Services/Login.service.test.tsx @@ -108,8 +108,14 @@ describe('Login.service', () => { it('should make expected API calls', async () => { await firstValueFrom(svc.setLoggedOut()); - expect(mockFromFetch).toHaveBeenCalledTimes(1); - expect(mockFromFetch).toHaveBeenNthCalledWith(1, `./api/v2.1/logout`, { + expect(mockFromFetch).toHaveBeenCalledTimes(2); + expect(mockFromFetch).toHaveBeenNthCalledWith(1, `./api/v2.1/auth`, { + credentials: 'include', + mode: 'cors', + method: 'POST', + body: null, + }); + expect(mockFromFetch).toHaveBeenNthCalledWith(2, `./api/v2.1/logout`, { credentials: 'include', mode: 'cors', method: 'POST',