2017-08-10 2 views
0

私自身の認証を実装しようとしましたが、セッションストレージにトークンを保存します。各ページで 私がチェック:Aureliaルーターのナビゲーションが機能しません

attached() { 

     if (sessionStorage.getItem("token") == null) { 
      console.log("sessionStorage null"); 
      this.theRouter.navigate("login"); 
     } 
     console.log("continue on transactions"); 
.... 
} 

結果 - 私は、コンソールにメッセージのsessionStorageのヌルを参照してくださいが、ナビゲートが動作しないと私はがトランザクションもに継続メッセージを参照してください。

app.tsでの私のルート(メインファイル)は以下の通りである。

私は正しいリダイレクトを整理することができますどのように
config.map([{ 
      route: ['', 'home'], name: 'home', moduleId: PLATFORM.moduleName('../home/home'), nav: true, title: 'Home' 
     },{ 
      route: 'transactions', name: 'transactions', moduleId: PLATFORM.moduleName('../transactions/transactions'), nav: true, title: 'Transactions' 
     }, { 
     route: 'login', name: 'login', moduleId: PLATFORM.moduleName('../auth/login'), nav: false, title: 'Login' 
    }]); 

ありがとうございます。

答えて

2

navigateの呼び出し後にreturnステートメントが機能の実行を停止する必要があると推測します。あなたは、コードの重複を減らすためにこれを行うには、ルータのパイプラインのフックを使用して見なければならない言われていること

...

http://aurelia.io/hub.html#/doc/article/aurelia/router/latest/router-configuration/8ここ

は、そのリンクから直接例です:

import {Redirect} from 'aurelia-router'; 

export class App { 
    configureRouter(config, router) { 
    var step = new AuthorizeStep; 
    config.addAuthorizeStep(step) 
    config.map([ 
     { route: ['', 'home'],  name: 'home',  moduleId: 'home/index' }, 
     { route: 'users',   name: 'users',  moduleId: 'users/index', settings: { auth: true } }, 
     { route: 'users/:id/detail', name: 'userDetail', moduleId: 'users/detail', settings: { auth: true } }, 
     { route: 'files/*path',  name: 'files',  moduleId: 'files/index', href:'#files', nav: true } 
    ]); 
    } 
} 

class AuthorizeStep { 
    run(navigationInstruction, next) { 
    if (navigationInstruction.getAllInstructions().some(i => i.config.settings.auth)) { 
     var isLoggedIn = // insert magic here; 
     if (!isLoggedIn) { 
     return next.cancel(new Redirect('login')); 
     } 
    } 

    return next(); 
    } 
} 
+0

感謝あなたのソリューションのために。 残念ながら、AuthorizeStepクラスを追加した後も問題は繰り返されています。 私は、標準的な決定に従って問題を解決しました。必要なルートごとに_auth = true_を追加しました。 –

関連する問題