diff --git a/src/app/guards/redirect-if-authenticated.guard.ts b/src/app/guards/redirect-if-authenticated.guard.ts index 25fbf1e..c83b803 100644 --- a/src/app/guards/redirect-if-authenticated.guard.ts +++ b/src/app/guards/redirect-if-authenticated.guard.ts @@ -24,6 +24,7 @@ export class RedirectIfAuthenticatedGuard { return this.authService.checkAuthUser().pipe( map((isAuthenticated) => { if (isAuthenticated) { + this.router.navigate(['/summary']); return false; } return true; diff --git a/src/app/shared/components/header/header.component.ts b/src/app/shared/components/header/header.component.ts index 4b76346..930f676 100644 --- a/src/app/shared/components/header/header.component.ts +++ b/src/app/shared/components/header/header.component.ts @@ -6,6 +6,7 @@ import { TranslateModule } from '@ngx-translate/core'; import { FirebaseService } from '../../../services/firebase.service'; import { AuthService } from '../../../services/auth.service'; import { ApiService } from '../../../services/api.service'; +import { catchError, of, switchMap } from 'rxjs'; @Component({ selector: 'app-header', @@ -33,21 +34,22 @@ export class HeaderComponent { } /** - * Loads the current user data by calling the getCurrentUserId method of the AuthService. + * Loads the current user data and sets it to the `currentUser` property. */ loadCurrentUser(): void { - this.authService.getCurrentUserId().subscribe((userId) => { - if (userId) { - this.apiService.getUserById(userId).subscribe( - (userData) => { - this.currentUser = userData; - }, - () => { - this.currentUser = {}; - } - ); - } - }); + this.authService + .getCurrentUserId() + .pipe( + switchMap((userId) => { + if (!userId) return of(null); + return this.apiService + .getUserById(userId) + .pipe(catchError(() => of(null))); + }) + ) + .subscribe((userData) => { + this.currentUser = userData || {}; + }); } /**