1

I'm trying to use a canActivate guard to validate url params and if they aren't a match then the app is supposed to redirect. I have the following so far:

@Injectable()

export class NodeInfoDetailGuard implements CanActivate {

    constructor(
        private _store: Store<NodeInfoRouteState>,
        private _router: Router,
    ) {}

    canActivate(route: ActivatedRouteSnapshot): Observable<boolean | UrlTree> {
        const {
            systemId,
            nodeName,
        } = route.params;

        const itemIsMatch = (item: ResourceResponseItem<NodeInfo>) =>
            item && item.payload.systemId === systemId && item.payload.nodeName === nodeName;

        // If nodeName or systemId not present, parses a string to a UrlTree
        if (!systemId || !nodeName) {
            return of(this._router.parseUrl('/node-info'));

        }
        // Otherwise, validate route parameters and check for match
        this._store.dispatch(new SelectNodeInfoAction({ systemId, nodeName }));
        return this._store.pipe(
            select(getNodeInfoSelectedItem),
            filter((item: ResourceResponseItem<NodeInfo>, index: number) => index >= 1 || itemIsMatch(item)),
            map(item => itemIsMatch(item) || this._router.parseUrl('/node-info')),
        );
    }
}

This works if I have the app already running in the browser, the redirect doesn't work if I'm using a fresh tab. The app attempts to dispatch from the store if the the nodeName or systemId are not a match

harkesh kumar
  • 833
  • 2
  • 13
  • 35

0 Answers0