我有解析器和守卫。在我的基本路由路径上,我有解析器,它可以进行 http 调用 - 获取用于身份验证的令牌。
在儿童路线内,我有警卫的许可。始终守卫在解析器之前执行 - 所以问题是首先执行我的守卫,然后执行 - 我的守卫。
所以解析器看起来像这样:
import { Injectable, Injector } from '@angular/core';
import { Resolve, RouterLink, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';
import { AuthStoreService } from '@services/auth-store.service';
import { LoginService } from 'src/api/controllers/Login';
import { OAuthService } from 'angular-oauth2-oidc';
@Injectable()
export class TokenResolver implements Resolve<any> {
constructor(
public injector: Injector,
public authStore: AuthStoreService,
public loginService: LoginService,
private oauthService: OAuthService,
public router: Router
) { }
resolve(): Observable<any> {
let resolver = this.loginService.token({ token: this.oauthService.getAccessToken() }).pipe(
tap(response => {
console.log('res', response);
this.authStore.setData(response);
this.authStore.isSetDataCalled = true;
return response;
},
), catchError(err => {
console.log('err', err);
this.router.navigate(['/405']);
return Observable.throw(err);
}));
console.log('resolver', resolver);
return resolver;
}
}
我怎么能在我的守卫中等待电话
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router } from '@angular/router';
import { AuthStoreService } from '@core/services/auth-store.service';
@Injectable()
export class HasPermissionGuard implements CanActivate {
constructor(
public authStore: AuthStoreService,
private router: Router // Use this to redirect to 403
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (!this.authStore.hasUserInfo()) {
return true;
}
if (this.authStore.hasPermission(route.data.permission)) {
return true;
}
this.router.navigate(['/403']); // Toggle this to redirect to 403 route
return false;
}
}