2016-08-07 7 views
4

ルータに承認パイプラインステップを追加しました。すべて正常に動作しますが、Redirectクラスを使用してユーザーをログインページに誘導すると、引数としてURLが使用されます。 Router.navigateToRoute()を使用していた場合は、ルート名を渡すことをお勧めします。これは可能ですか?いくつかのグーグル後Aurelia Redirectクラスの使用

@inject(AuthService) 
class AuthorizeStep { 
    constructor (authService) { 
     this.authService = authService; 
    } 

    run (navigationInstruction, next) { 
     if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) { 
      if (!this.authService.isLoggedIn) { 
       return next.cancel(new Redirect('url-to-login-page')); // Would prefer to use the name of route; 'login', instead. 
      } 
     } 

     return next(); 
    } 
} 

答えて

7

私は、ルータ名(とオプションのparams)を取り、URLを返すRouter.generate()方法を見つけました。

@inject(Router, AuthService) 
class AuthorizeStep { 
    constructor (router, authService) { 
     this.router = router; 
     this.authService = authService; 
    } 

    run (navigationInstruction, next) { 
     if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) { 
      if (!this.authService.isLoggedIn) { 
       return next.cancel(new Redirect(this.router.generate('login'))); 
      } 
     } 

     return next(); 
    } 
} 

編集:私は今、次のように私にauthorizeステップを更新した私はRedirectToRouteクラスを発見したいくつかのより多くのグーグルでの後。

import { RedirectToRoute } from 'aurelia-router'; 

...

return next.cancel(new RedirectToRoute('login')); 
関連する問題