Skip to content

Commit

Permalink
fix(auth): render username in top bar (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores authored Jun 3, 2024
1 parent 759e8ae commit 056ebf8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/app/Shared/Services/Login.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>((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<string> {
Expand Down
2 changes: 1 addition & 1 deletion src/mirage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 8 additions & 2 deletions src/test/Shared/Services/Login.service.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 056ebf8

Please sign in to comment.