【发布时间】:2022-02-09 20:03:44
【问题描述】:
如果用户的状态是complete,我有一个函数可以将用户重定向到页面Orders,如果用户的状态不是complete,则重定向到HOME 页面。
Orders 页面有一个实现canActivate 的GuardService,而HOME 页面没有任何保护。
我有一些拥有R_ORDERS 权限且处于complete 状态的用户根本没有导航。 (即它们既不会被路由到Orders 页面,也不会被路由到HOME 页面)。
我可以看到检索用户权限的 HTTP 请求被调用,但它们被卡在该页面中。
我的问题与这篇文章有关吗? enter link description here
或这篇文章 enter link description here
另外,我尝试在 observable 完成时添加日志,但在这种情况下,日志永远不会显示在控制台中。
return this.getList().subscribe((e) => {
const right = new List(e).FirstOrDefault(
(d) => d.code == securizedPath.keyCode
)?.type;
if (!right) return resolve("OFF");
return resolve(right);
},
(error) => {
console.log("Right store threw an error");
console.log(error);
},
() => {
console.log("Right list completed")
}
);
感谢您的帮助
async redirectTo() {
this.loading = true;
this.userState = await this.stateService.getToPromise(this.userId);
if (this.userState.isComplete) {
this.orderService.init();
this.router.navigate([RouteNames.ORDERS]);
this.loading = false;
return;
}
this.router.navigate([RouteNames.HOME]);
return;
}
RouteNames.ORDERS 有以下守卫:
@Injectable({
providedIn: "root",
})
export class GuardService implements CanActivate {
constructor(
private router: Router,
private rightService: UserRightService
) {}
canActivate(
route: import("@angular/router").ActivatedRouteSnapshot,
state: import("@angular/router").RouterStateSnapshot
):
| boolean
| import("@angular/router").UrlTree
| import("rxjs").Observable<boolean | import("@angular/router").UrlTree>
| Promise<boolean | import("@angular/router").UrlTree> {
return this.test(route);
}
async test(route: ActivatedRouteSnapshot) {
const right = await this.rightService.isRouteAllowed(route.url[0].path);
if (right == "OFF") {
this.router.navigate([RouteNames.FEATURE_UNAUTHORIZED]);
return false;
} else if (right != "ON") {
this.router.navigate([RouteNames.FEATURE_UNAUTHORIZED]);
return false;
}
return true;
}
}
UserRightService 的代码是
isRouteAllowed(route: string): Promise<Right> {
const securizedPath = new List(this.securizedPaths).FirstOrDefault(
(e) => e.name == route
);
return new Promise((resolve) => {
return this.getList().subscribe((e) => {
const right = new List(e).FirstOrDefault(
(d) => d.code == securizedPath.keyCode
)?.type;
if (!right) return resolve("OFF");
return resolve(right);
});
});
}
securizedPaths 定义如下
typescript[{ name: RouteNames.ORDERS, keyCode: "R_ORDERS" }, // other value ]
getList 是
getList() {
this.subjectList = new ReplaySubject<T[]>();
this.load();
return this.subjectList;
}
protected load() {
return this.getService()
.getAll()
.then((e) => {
this.subjectList.next(e);
})
.catch((e) => {
this.subjectList.next([]);
this.subjectList = null;
});
}
// Code for this.getService().getAll()
async getAll(id?: any, parameter?: any): Promise<Array<T>> {
let urlParameters;
({ urlParameters, parameter } = this.createParameters(parameter, id));
return this.http
.get(this.url + (urlParameters ? "?" + urlParameters : ""))
.toPromise<Array<T>>();
}
【问题讨论】:
标签: angular rxjs angular-ui-router observable angular-router-guards