【问题标题】:How to redirect to another router from an AuthorizedStep如何从授权步骤重定向到另一个路由器
【发布时间】:2017-11-25 19:49:23
【问题描述】:

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

在我的授权路由器中,我有一个 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() 然后直接去另一个路由器...我不想重定向..

如何取消promise中的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()? (如果这是问题)

但我真正想要的是……

我如何停止这个路由器并干净地移动到另一个路由器?

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    正如你所做的那样,我一直在尝试。这对我很有用,因为我相信 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')));
    }
    

    ...没有错误。

    【讨论】:

      猜你喜欢
      • 2017-12-20
      • 1970-01-01
      • 2017-03-18
      • 2020-07-16
      • 2017-12-18
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多