diff --git a/packages/router/src/operators/check_guards.ts b/packages/router/src/operators/check_guards.ts new file mode 100644 index 0000000000..160c266907 --- /dev/null +++ b/packages/router/src/operators/check_guards.ts @@ -0,0 +1,27 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Injector, Type} from '@angular/core'; +import {Observable, OperatorFunction} from 'rxjs'; +import {mergeMap} from 'rxjs/operators'; + +import {Route} from '../config'; +import {PreActivation} from '../pre_activation'; +import {recognize as recognizeFn} from '../recognize'; +import {ChildrenOutletContexts} from '../router_outlet_context'; +import {RouterStateSnapshot} from '../router_state'; +import {UrlTree} from '../url_tree'; + +export function checkGuards( + rootContexts: ChildrenOutletContexts, currentSnapshot: RouterStateSnapshot, + moduleInjector: Injector, preActivation: PreActivation): OperatorFunction { + return function(source: Observable) { + return source.pipe( + mergeMap((appliedUrl): Observable => { return preActivation.checkGuards(); })); + }; +} \ No newline at end of file diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 83f474efc5..bce48239be 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -17,6 +17,7 @@ import {createUrlTree} from './create_url_tree'; import {ActivationEnd, ChildActivationEnd, Event, GuardsCheckEnd, GuardsCheckStart, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, NavigationTrigger, ResolveEnd, ResolveStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events'; import {applyRedirects} from './operators/apply_redirects'; import {beforePreactivation} from './operators/before_preactivation'; +import {checkGuards} from './operators/check_guards'; import {recognize} from './operators/recognize'; import {setupPreactivation} from './operators/setup_preactivation'; import {PreActivation} from './pre_activation'; @@ -735,21 +736,24 @@ export class Router { map(preActivation => ({...p, preActivation})))), tap(p => preActivation = p.preActivation)); - const preactivationCheckGuards$ = - preactivationSetup$.pipe(mergeMap((p): Observable => { - if (typeof p === 'boolean' || this.navigationId !== id) return of (false); - const {appliedUrl, snapshot} = p; - - this.triggerEvent(new GuardsCheckStart( - id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot)); - - return preActivation.checkGuards().pipe(map((shouldActivate: boolean) => { - this.triggerEvent(new GuardsCheckEnd( - id, this.serializeUrl(url), this.serializeUrl(appliedUrl), snapshot, - shouldActivate)); - return {appliedUrl: appliedUrl, snapshot: snapshot, shouldActivate: shouldActivate}; - })); - })); + const preactivationCheckGuards$: Observable = + preactivationSetup$.pipe(mergeMap( + p => this.navigationId !== id ? of (false) : of (p.appliedUrl) + .pipe( + tap(_ => this.triggerEvent(new GuardsCheckStart( + id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl), + p.snapshot))), + checkGuards( + this.rootContexts, this.routerState.snapshot, this.ngModule.injector, + preActivation), + tap(shouldActivate => this.triggerEvent(new GuardsCheckEnd( + id, this.serializeUrl(url), this.serializeUrl(p.appliedUrl), + p.snapshot, shouldActivate))), + map(shouldActivate => ({ + appliedUrl: p.appliedUrl, + snapshot: p.snapshot, + shouldActivate: shouldActivate + }))))); const preactivationResolveData$ = preactivationCheckGuards$.pipe(mergeMap((p): Observable => {