Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 relative route navigate in a guard

We have child routes definitions presented here, in which we use a guard to check if the user has accepted terms before using our service.

account/secret/secret.routes.ts:

import { Routes } from '@angular/router';
import { SecretFormComponent } from './secret-form.component';
import { SecretTermsComponent } from './secret-terms.component';
import { TermsGuard } from './services/terms-guard.service';

export const secretRoutes: Routes = [
  {
    path: '',
    redirectTo: 'form'
  },
  {
    path: 'form',
    component: SecretFormComponent,
    canActivate: [TermsGuard]
  },
  { path: 'terms', component: SecretTermsComponent }

  // otherwise redirect to form
  { path: '**',  redirectTo: 'form' }
];

In our terms-guard, we have defined this code:

this.router.navigate(['/account/secret/terms']);
return false;

Is there a way to redirect using relative route navigation from within a "routing group"? Because defining an absolute path may be break if one day our account website dashboard is renamed to anything else like my-account for example. We want our secret module to be reusable.

I'd like to be able to navigate to ['./terms'] in my guard but it doesn't work, just like the guard doesn't know "from where" to start relative navigation.

like image 266
BlackHoleGalaxy Avatar asked Dec 06 '16 12:12

BlackHoleGalaxy


People also ask

Which route Guard is helpful in preventing Unauthorised access to a component?

That is called AuthGuard. AuthGuard is used to protect the routes from unauthorized access.


2 Answers

What you want to use is the RouterStateSnapshot parameter of the canActivate method of your guard.

The RouterStateSnapshot has the current path you are in the process of routing to, whereas the Router.url is the current path your are routing from (which is why @Günter Zöchbauer's answer doesn't quite work in this scenario).

For this to apply completely to your use case without additional code you will want to change your routes to

/account/secret/form and /account/secret/form/terms

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ChildRouteGuard implements CanActivate {
    constructor(private router: Router) { }

    canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        // user can view the specific route
        if (...) {
            return true;
        }

        // else route them to a variation of the route
        this.router.navigate([state.url, 'terms']);

        return false;
    }
}

Edit 1

You could keep your current url structure by also taking advantage of the ActivatedRouteSnapshot as seen below

Assuming routes like account/secret/form and account/secret/terms

route.url is an array of url segments, we want the last one because that is going to be form. We find the index of that in the current route we are trying to go to account/secret/form and slice it out. At this point we have the 'parent' of the current route we are trying to navigate to account/secret and we can add on the sibling to form which is terms.

Note: this pattern breaks down when you have repeated occurrences of form in your url

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ChildRouteGuard implements CanActivate {
    constructor(private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        // user can view the specific route
        if (...) {
            return true;
        }

        let parentUrl = state.url
            .slice(0, state.url.indexOf(route.url[route.url.length - 1].path));

        this.router.navigate([parentUrl, 'terms']);

        return false;
    }
}
like image 164
seangwright Avatar answered Oct 01 '22 07:10

seangwright


You should be able to get the url you were attempting to navigate to from the state.url, parse it by using the provided urlSerializer, then splice in the new urlSegment you want relative to what was matched by the guard.

export class TermsGuard implements CanActivate<SecretFormComponent>{
    constructor(private router: Router, private urlSerializer: UrlSerializer) { }
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        let routerStateUrlTree = this.urlSerializer.parse(state.url);
        let newSegment = new UrlSegment('terms', {});
        routerStateUrlTree.root.children.primary.segments.splice(-1, 1, newSegment);
        this.router.navigateByUrl(routerStateUrlTree);
        return false;
    }
} 

You said "I'd like to be able to navigate to ['./terms']" but based on your routes I think you meant ['../terms'], but if you did mean ['./terms']just change the second parameter of the splice to a 0 so it doesn't remove the last segment.

like image 27
Patrick Graham Avatar answered Oct 01 '22 08:10

Patrick Graham