簡體   English   中英

Angular 5.1應用程序中的一個組件不會觸發canActivate保護

[英]canActivate guard is not triggered for one component in Angular 5.1 app

在我的Angular 5.1.1應用程序中,未觸發單個組件上的canActivate保護。 當我路由到“用戶”時,不會觸發AuthenticationGuard,但它適用於所有其他路徑。

我認為這與“用戶”用於路徑為空時重定向到。 所以我將redirectTo屬性更改為client。 第一次不觸發AuthGuard,但始終觸發。 對於“用戶”,它仍然不會觸發。

路線

const routes: Routes = [
    { path: 'error', component: ErrorComponent /* , canActivate: [AuthenticationGuard] */  },
    { path: 'id_token', component: OAuthCallbackComponent, canActivate: [OAuthCallbackHandler] },                   // reply URL AAD
    { path: 'client', component: ClientsComponent, canActivate: [AuthenticationGuard] },
    { path: 'colleagues', component: ColleaguesComponent, canActivate: [AuthenticationGuard]},
    { path: 'user', component: UserComponent, canActivate: [AuthenticationGuard] },
    { path: 'useroverview', component: UserOverviewComponent, canActivate: [AuthenticationGuard] },
    { path: 'login', component: LoginComponent },
    { path: '', redirectTo: 'user', pathMatch: 'full', canActivate: [AuthenticationGuard] },
    { path: 'loading', component: OAuthCallbackComponent },
    { path: 'noaccess', component: NoAccessComponent },
    { path: '**', component: NoAccessComponent }
];

AuthGuard

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        const navigationExtras: NavigationExtras = {
            queryParams: { 'redirectUrl': route.url }
        };

        if (!this.adalService.userInfo || !this.adalService.accessToken) {
            sessionStorage.clear();
            sessionStorage.setItem('redirect', this.setRedirectUrl(route.url));
            this.router.navigate(['login'], navigationExtras);
            return false;
        }

        if (((route.routeConfig.path === "client" || route.routeConfig.path === "useroverview") && UserRights[sessionStorage.getItem('Role')] < 3) ||
            ((sessionStorage.getItem('UserType') === '66' || sessionStorage.getItem('UserType') === '74') && route.routeConfig.path === "colleagues") && UserRights[sessionStorage.getItem('Role')] === 0)
        {
            this.router.navigate(['404']);
            return false;
        }

        if (sessionStorage.length === 0) {
            this.spinner.show();
            return this.userManService.getUserInfo().switchMap(resp => {
                this.sessionUser(resp);
                return this.userManService.getMyAccountData().map(data => {
                    this.setUserInfo(data);
                    $(document).trigger('Authenticated');
                    this.spinner.hide();
                    this.myMenuOption();
                    return true;
                }).catch(() => {
                    this.spinner.hide();
                    return Observable.of(false);
                });
            }).catch(() => {
                this.spinner.hide();
                return Observable.of(false);
            });
        }
        else
            return Observable.of(true);
    }

怎么可能不為該組件觸發AuthGuard?

我錯過了一件非常明顯的事情(太愚蠢了!)。 在用戶路徑上,我還分配了子級。 因此,當我在此處設置canActivate守護程序時,它工作正常。

const routes: Routes = [{
path: 'user',
component: UserComponent,
canActivate: [AuthenticationGuard],
children: [
    { path: 'invite/:key', component: ModalContainerComponent, canActivate: [AuthenticationGuard] }
]
}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM