fix: improved guard routing and optimized user data loading
This commit is contained in:
parent
47c7aacbfb
commit
0edfafb5f6
2 changed files with 16 additions and 13 deletions
|
|
@ -24,6 +24,7 @@ export class RedirectIfAuthenticatedGuard {
|
|||
return this.authService.checkAuthUser().pipe(
|
||||
map((isAuthenticated) => {
|
||||
if (isAuthenticated) {
|
||||
this.router.navigate(['/summary']);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -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,20 +34,21 @@ 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 || {};
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue