0
我遵循Aurelia documentation關於添加導航管道步驟。導航管道奧裏利亞
我已經創建了我的身份驗證服務和AuthRouterPipelineStep:
import {inject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';
import {Router} from 'aurelia-router';
import {AuthResult} from './authResult';
import {Redirect} from 'aurelia-router';
@inject(HttpClient, Router)
export class Auth {
constructor(httpClient, router) {
this.httpClient = httpClient;
this.router = router;
this.internalIsLoggedIn = false;
}
login(username, password) {
if (username === "callum" && password === "password") {
this.router.navigate('products');
this.internalIsLoggedIn = true;
}
return new AuthResult("Unable to login.");
}
get isLoggedIn() { return this.internalIsLoggedIn; }
}
@inject(Auth)
export class AuthRouterPipelineStep {
constructor(auth) {
this.auth = auth;
}
run(navigationInstruction, next) {
console.log("Navigating");
if (navigationInstruction
.getAllInstructions()
.some(i => i.config.settings.roles.indexOf('public') === -1))
{
var isLoggedIn = this.auth.isLoggedIn;
if (!isLoggedIn) {
return next.cancel(new Redirect('welcome'));
}
}
return next();
}
}
而在我的應用我已經配置了一切:
import {Auth, AuthRouterPipelineStep} from './auth/auth';
import {inject} from 'aurelia-framework';
import {Redirect} from 'aurelia-router';
@inject(Auth)
export class App {
constructor(auth) {
this.auth = auth;
}
get isLoggedIn() { return this.auth.isLoggedIn; }
configureRouter(config, router) {
config.title = 'Reaper';
config.addPipelineStep('authorise', AuthRouterPipelineStep);
config.map([
{ route: ['', 'welcome'], name: 'welcome', moduleId: 'welcome', nav: true, title: 'Home', settings: { icon: 'fa-home', roles: ['public'] } },
{ route: 'contacts', name: 'contacts', moduleId: './contacts/index', nav: true, title: 'Contacts', settings: { icon: 'fa-' } },
{ route: 'companies', name: 'companies', moduleId: './companies/index', nav: true, title: 'Companies', settings: { icon: 'fa-' } },
{ route: 'products', name: 'products', moduleId: './products/index', nav: true, title: 'Products', settings: { icon: 'fa-' } }
]);
this.router = router;
}
}
我上的導航管道一步Run
功能有console.log
。那永遠不會被調用。我錯過了什麼......?
我知道,所有的導航步驟是由容器注入的,所以你可以在構造函數中有依賴...