如何从AuthorizedStep

时间:2017-11-25 10:15:08

标签: aurelia

我有两条路线 - 一条是公共路由器,另一条是授权路由器。

在我的授权路由器中,我有一个authorizeStep。

authorizedStep检查localStorage中是否有令牌,以及是否返回next()。

但是,如果找不到它应该停止的令牌并跳出来返回公共路由器。

我无法停止授权步骤,而是转到公共路由器。

我有:

run(navigationInstruction: NavigationInstruction, next: Next): Promise<any> {
      return Promise.resolve()
        .then(() => this.checkSessionExists(navigationInstruction, next)
        .then(result => result || next())
    );

 }
checkSessionExists(navigationInstruction: NavigationInstruction, next: Next) {
    const session = this.authService.getIdentity();
    if (!session) {
    // HOW DO I CANCEL THE NEXT HERE AND GO TO THE PUBLIC ROUTER?
        return next.cancel(new Redirect('login'))
    }

    return next()

}

forceReturnToPublic() {
    this.authService.clearIdentity();
    this.router.navigate("/", { replace: true, trigger: false });
    this.router.reset();
    this.aurelia.setRoot("public/public/public");
}

我有函数forceReturnToPublic()但是我想去取消next()然后直接转到其他路由器......我不想重定向..

如何取消承诺中的下一个并重置路由器?

这是我的boot.ts应该重新开放给公众,但我不知道如何干净地跳出承诺......

// After starting the aurelia, we can request the AuthService directly
// from the DI container on the aurelia object. We can then set the 
// correct root by querying the AuthService's checkJWTStatus() method
// to determine if the JWT exists and is valid.
aurelia.start().then(() => {
    var auth = aurelia.container.get(AuthService);
    let root: string = auth.checkJWTStatus() ? PLATFORM.moduleName('app/app/app') : PLATFORM.moduleName('public/public/public');

    aurelia.setRoot(root, document.body)
});

如果我将forceReturnToPublic()放在return next.cancel(new Redirect('login')的位置,它会进入无限循环并出现错误。

修改

我发现THIS问题表明我应该添加“this.pipeLineProvider.reset()”,所以我做了 - 就像这样...

forceReturnToPublic() {
    this.pipelineProvider.reset();
    this.authService.clearIdentity();
    this.router.navigate("/", { replace: true, trigger: false });
    this.router.reset();
    this.aurelia.setRoot("public/public/public");
}

虽然它直接回到公共路线,但我在控制台中收到错误。

aurelia-logging-console.js:47 ERROR [app-router] Error: There was no router-view found in the view for ../components/clients/clientList/clientList.
at _loop (aurelia-router.js:281)
at NavigationInstruction._commitChanges (aurelia-router.js:307)
at CommitChangesStep.run (aurelia-router.js:143)
at next (aurelia-router.js:112)
at iterate (aurelia-router.js:1272)
at processActivatable (aurelia-router.js:1275)
at ActivateNextStep.run (aurelia-router.js:1161)
at next (aurelia-router.js:112)
at iterate (aurelia-router.js:1191)
at processDeactivatable (aurelia-router.js:1194)

我点击了具有路由器视图的clientList导航链接..

我如何/在哪里放置pipelineProvider.reset()? (如果这是问题)

但我真正想要的是......

如何停止此路由器并干净地移动到另一台路由器?

1 个答案:

答案 0 :(得分:0)

正如你所做的那样,我一直在尝试。这对我有用,因为我相信在运行到下一步之前需要完成pipelineProvider。所以我使用了这样的承诺:

forceReturnToPublic(): Promise<any> {
    return Promise.resolve()
        .then(() => this.pipelineProvider.reset())
        .then(() => this.authService.clearIdentity())
        .then(() => this.router.navigate("/", { replace: true, trigger: false }))
        .then(() => this.router.reset())
        .then(() => this.aurelia.setRoot(PLATFORM.moduleName('public/public/public')));
}

......并且没有错误。