[DURACOM-234] Migrate to functional guards WIP

This commit is contained in:
Giuseppe Digilio
2024-03-29 18:28:32 +01:00
parent 5dbc6ce7a8
commit 29f5a17528
24 changed files with 368 additions and 474 deletions

View File

@@ -1,4 +1,9 @@
import { Injectable } from '@angular/core';
import { inject } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivateFn,
RouterStateSnapshot,
} from '@angular/router';
import {
select,
Store,
@@ -19,24 +24,16 @@ import { isAuthenticationBlocking } from './selectors';
* route until the authentication status has loaded.
* To ensure all rest requests get the correct auth header.
*/
@Injectable({
providedIn: 'root',
})
export class AuthBlockingGuard {
export const AuthBlockingGuard: CanActivateFn = (
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
store: Store<AppState> = inject(Store<AppState>),
): Observable<boolean> => {
return store.pipe(select(isAuthenticationBlocking)).pipe(
map((isBlocking: boolean) => isBlocking === false),
distinctUntilChanged(),
filter((finished: boolean) => finished === true),
take(1),
);
};
constructor(private store: Store<AppState>) {
}
/**
* True when the authentication isn't blocking everything
*/
canActivate(): Observable<boolean> {
return this.store.pipe(select(isAuthenticationBlocking)).pipe(
map((isBlocking: boolean) => isBlocking === false),
distinctUntilChanged(),
filter((finished: boolean) => finished === true),
take(1),
);
}
}