fix: improved guard routing and optimized user data loading

This commit is contained in:
Chneemann 2025-03-25 09:52:46 +01:00
parent 47c7aacbfb
commit 0edfafb5f6
2 changed files with 16 additions and 13 deletions

View file

@ -24,6 +24,7 @@ export class RedirectIfAuthenticatedGuard {
return this.authService.checkAuthUser().pipe(
map((isAuthenticated) => {
if (isAuthenticated) {
this.router.navigate(['/summary']);
return false;
}
return true;

View file

@ -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 || {};
});
}
/**