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(
|
return this.authService.checkAuthUser().pipe(
|
||||||
map((isAuthenticated) => {
|
map((isAuthenticated) => {
|
||||||
if (isAuthenticated) {
|
if (isAuthenticated) {
|
||||||
|
this.router.navigate(['/summary']);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import { TranslateModule } from '@ngx-translate/core';
|
||||||
import { FirebaseService } from '../../../services/firebase.service';
|
import { FirebaseService } from '../../../services/firebase.service';
|
||||||
import { AuthService } from '../../../services/auth.service';
|
import { AuthService } from '../../../services/auth.service';
|
||||||
import { ApiService } from '../../../services/api.service';
|
import { ApiService } from '../../../services/api.service';
|
||||||
|
import { catchError, of, switchMap } from 'rxjs';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-header',
|
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 {
|
loadCurrentUser(): void {
|
||||||
this.authService.getCurrentUserId().subscribe((userId) => {
|
this.authService
|
||||||
if (userId) {
|
.getCurrentUserId()
|
||||||
this.apiService.getUserById(userId).subscribe(
|
.pipe(
|
||||||
(userData) => {
|
switchMap((userId) => {
|
||||||
this.currentUser = userData;
|
if (!userId) return of(null);
|
||||||
},
|
return this.apiService
|
||||||
() => {
|
.getUserById(userId)
|
||||||
this.currentUser = {};
|
.pipe(catchError(() => of(null)));
|
||||||
}
|
})
|
||||||
);
|
)
|
||||||
}
|
.subscribe((userData) => {
|
||||||
|
this.currentUser = userData || {};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue