2017-11-25 115 views
0

我有两个途径 - 一个是公共路由器,另一个是授权的路由器。如何重定向到另一个路由器从AuthorizedStep

在我的授权路由器中,我有一个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(),但是我想去并取消下一个(),然后直接去其他路由器......我不想重定向..

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

这里是我的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()代替的返回next.cancel(新重定向(“登录”),它进入与错误无限循环。

编辑

我发现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) 

我点击客户端列表导航链接里面确实有一个路由器视图上..

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

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

如何停止该路由器和干净移动到另一台路由器?

回答

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'))); 
} 

..没有错误。

相关问题