1
votes

I have angular universal app where part of routes is protected by CanActivate guard, where canActivate method use services to protect the route. But with initialNavigation: "enabled" config my guards didn't work. As I suppose, this issue is faced because of:

The initial navigation starts before the root component is created. The bootstrap is blocked until the initial navigation is complete. ( from angular docs )

The question is: how can I use services in canActivate guard with initialNavigation - enabled?

1
What do you mean by "guards didn't work"? I did use guards calling services with initialNavigation set to enabled in one of my projects and it did work - David
CanActivate Method in my guard looks like: canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { return this._statusService .userStatusObservable .pipe( map(status => { if(status.is_authenticated && status.is_customer) { return true; } else if(!status.is_authenticated) { setTimeout(() => this._router.navigate([])); return false; } } }) ); } and maybe it does not work because of The bootstrap is blocked until the initial navigation is complete. - Yuriy Kovalets
My mistake, I block request at this service by using something like if( isPlatformBrowser(this._platformId) ) return null thanks all for answering - Yuriy Kovalets

1 Answers

0
votes

From what you provided, I could only get into a little of data.

I think what you need is to set some routes, to be protected with can activate, and some other without the route guard.

You can do something like this.

{path: 'secure-path', component: 'SomeSecureComponent', canActivate: [AuthGuard]},
{path: 'not-a-secure-path', component: 'SomeNonSecureComponent'},

You can do something like this to get rid of the route guard in the insecure pages, just don't provide them.

If you think I got your question wrong, please let me know, because this is what i understood from the little information you provided.