2016-07-19 33 views
2

我想在我的應用中使用aurelia-auth,並且還有一個與應用其餘部分完全分離的登錄頁面。我一直在這個鏈接下面的教程:https://auth0.com/blog/2015/08/05/creating-your-first-aurelia-app-from-authentication-to-calling-an-api/重置根目錄時Aurelia路由器不工作

我遇到的問題是我成功登錄後,嘗試路由到應用程序,沒有找到路線。無論我爲登錄重定向網址輸入什麼內容,我都找不到路由。

這裏是我的代碼:

app.js:

... 
import { Router } from 'aurelia-router'; 
import { AppRouterConfig } from './router-config'; 
import { FetchConfig } from 'aurelia-auth'; 

... 

@inject(..., Router, AppRouterConfig, FetchConfig) 
export class App { 
    constructor(router, appRouterConfig, FetchConfig) { 
     this.router = router; 
     this.appRouterConfig = appRouterConfig; 
     this.fetchConfig = fetchConfig; 

     ... 
    } 

    activate() { 
     this.fetchConfig.configure(); 
     this.appRouterConfig.configure(); 
    } 

    ... 

} 

login.js:

import { AuthService } from 'aurelia-auth'; 
import { Aurelia } from 'aurelia-framework'; 
... 

@inject(..., Aurelia, AuthService) 
export class LoginScreen { 
    constructor(..., aurelia, authService) { 
     this.aurelia = aurelia; 
     this.authService = authService; 
     ... 
    } 

    login() { 
     return this.authService.login(this.username, this.password) 
      .then(response => { 
       console.log("Login response: " + response); 
       this.aurelia.setRoot('app'); 
      }) 
      .catch(error => { 
       this.loginError = error.response; 
       alert('login error = ' + error.response); 
      }); 
    } 

    ... 

} 

main.js:

import config from './auth-config'; 
import { AuthService } from 'aurelia-auth'; 
import { Aurelia } from 'aurelia-framework'; 
... 

export function configure(aurelia) { 
    aurelia.use 
     .defaultBindingLanguage() 
     .defaultResources() 
     .developmentLogging() 
     .router() 
     .history() 
     .eventAggregator() 
     ... 
     .plugin('aurelia-auth', (baseConfig) => { 
      baseConfig.configure(config); 
     }); 


    let authService = aurelia.container.get(AuthService); 
    aurelia.start() 
     .then(a => { 
      if (authService.isAuthenticated()) { 
       a.setRoot('app'); 
      } else { 
       a.setRoot('login'); 
      } 
     }); 
} 

AUTH-配置。 js:

var config = { 
    baseUrl: 'http://localhost:3001', 
    loginUrl: 'sessions/create', 
    tokenName: 'id_token', 
    //loginRedirect: '#/home' //looks like aurelia-auth defaults to #/ which is fine for me 
} 

export default config; 

路由器config.js:

import { AuthorizeStep } from 'aurelia-auth'; 
import { inject } from 'aurelia-framework'; 
import { Router } from 'aurelia-router'; 

@inject(Router) 
export class AppRouterConfig { 
    constructor(router) { 
     this.router = router; 
    } 

    configure() { 
     console.log('about to configure router'); 

     var appRouterConfig = function (config) { 
      config.title = 'My App'; 

      config.addPipelineStep('authorize', AuthorizeStep); 

      config.map([ 
       { 
        route: ['', 'home'], 
        name: 'home', 
        moduleId: '.components/home/home', 
        nav: true, 
        title: 'Home', 
        auth: true 
       }, 
       { 
        route: ['employees'], 
        name: 'employees', 
        moduleId: './components/employees/employees', 
        nav: true, 
        title: 'Employees', 
        auth: true 
       } 
      ]); 

      this.router.configure(appRouterConfig); 
     } 
    }; 
} 

當加載應用程序,它成功地進入到登錄頁面,我能夠成功登錄並嘗試重定向,但我得到這個錯誤控制檯:

ERROR [app-router] Error: Route not found:/
    at AppRouter._createNavigationInstruction (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-router.js:1039:29) 
    at AppRouter.loadUrl (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-router.js:1634:19) 
    at BrowserHistory._loadUrl (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-history-browser.js:301:55) 
    at BrowserHistory.activate (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-history-browser.js:200:21) 
    at AppRouter.activate (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-router.js:1689:20) 
    at eval (http://127.0.0.1:8080/jspm_packages/npm/[email protected]0-rc.1.0.1/aurelia-router.js:1670:21) 
    at AppRouter.registerViewPort (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-router.js:1672:10) 
    at new RouterView (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/router-view.js:112:19) 
    at Object.invokeWithDynamicDependencies (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-dependency-injection.js:329:20) 
    at InvocationHandler.invoke (http://127.0.0.1:8080/jspm_packages/npm/[email protected]/aurelia-dependency-injection.js:311:168)error @ aurelia-logging-console.js:54log @ aurelia-logging.js:37error @ aurelia-logging.js:70(anonymous function) @ aurelia-router.js:1637 
aurelia-logging-console.js:54 ERROR [app-router] Router navigation failed, and no previous location could be restored. 

我在這裏搜索了很多答案,但很難找到很好的答案。任何人有任何想法?任何幫助表示讚賞!

回答

3

我認爲問題在於你在activate()方法裏面配置路由器。在我看來,沒有必要這樣做。

您可以重置根組件後,導航到路徑:

this.aurelia.setRoot('./login') 
    .then((aurelia) => { 
    aurelia.root.viewModel.router.navigateToRoute('someLoginRoute'); 
    }); 

您還可以使用mapUnknownRoutes,這是非常有用的:

configureRouter(config, router) { 
     config.title = "Super Secret Project"; 
     config.map([ 
      { route: [ '', 'screen-1'], moduleId: "./screen-1", nav: true, title: "Beginscherm" }, 
      { route: 'screen-2', name:'screen-2', moduleId: "./screen-2", nav: true, title: "Beginscherm" } 
     ]); 

     config.mapUnknownRoutes(instruction => { 
     return './screen-1'; 
     }); 

     this.router = router;   
    } 

見這個例子https://gist.run/?id=00b8b3745a480fb04184e8440e8be8c5。請注意登錄/註銷功能。

我希望這有助於!

+0

我現在不在我的計算機上試試你的advicr,但是如果我不在激活方法中,我應該在哪裏配置路由器? – A2345sooted

+0

你不能只使用configureRouter方法嗎?或者可能調用構造函數方法中的函數 –

+0

構造函數的方法工作!先生非常感謝您。我在這一塊上敲了敲頭。對於aurelia還是新的。我非常感謝幫助。它正在工作。 – A2345sooted

0

U可以通過重新加載或刷新應用程序來解決此問題。在設定應用程序 即 之後。 a.setRoot('app'); location.reload();

+0

這似乎並沒有工作,但我不知道我正在重新加載正確或在正確的位置......我設置應用程序根後添加document.location.reload(),我也試過在我調用appRouterConfig的配置之後把它放好......我也試過在router.config文件中配置this.router.configure文件 – A2345sooted

+0

,或者你可以像配置config.mapUnknownRoutes一樣配置ur ruter(指令=> instruction.config.moduleId ='。組件/家庭/家「; 返回指令; }) –