Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 4x 4x 4x 5x 4x 2x 2x 2x | import { AuthService } from '../services/auth/auth.service';
import { isPlatformBrowser } from '@angular/common';
import { inject, PLATFORM_ID } from '@angular/core';
import { CanActivateFn, Router, UrlTree } from '@angular/router';
import { isValidReturnUrl } from '@drevo-web/shared';
import { Observable } from 'rxjs';
import { filter, map, take } from 'rxjs/operators';
export const authGuard: CanActivateFn = (_route, state): Observable<boolean | UrlTree> | boolean => {
const platformId = inject(PLATFORM_ID);
if (!isPlatformBrowser(platformId)) {
return true;
}
const authService = inject(AuthService);
const router = inject(Router);
return authService.isLoading$.pipe(
filter(isLoading => !isLoading),
take(1),
map(() => {
if (authService.isAuthenticated) {
return true;
}
const returnUrl = isValidReturnUrl(state.url) ? state.url : '/';
return router.createUrlTree(['/login'], {
queryParams: { returnUrl },
});
})
);
};
|