aurelia重定向行给出错误"错误[app-router]错误:无法找到ID为:not-found"

时间:2017-11-25 05:45:14

标签: aurelia

我一直在实施授权步骤,我使用fragsalat对THIS问题的答案进行了建模。

一切正常,直到到达

       return new Redirect('login');

我得到错误:

aurelia-logging-console.js:47 ERROR [app-router] Error: Expected router pipeline to return a navigation result, but got [{"url":"login","options":{"trigger":true,"replace":true},"shouldContinueProcessing":false}] instead.
at processResult (aurelia-router.js:1761)
at aurelia-router.js:1725
at <anonymous>

我不确定为什么这不仅仅是重定向?

这是完整的app.ts文件,因此您可能会看到上下文:

    import { Aurelia, PLATFORM, autoinject } from "aurelia-framework";
    import {
        Redirect,
        NavigationInstruction,
        Router,
        RouterConfiguration,
        Next
    } from "aurelia-router";

    import { AuthService } from "../../auth/auth-service";
    //import { Clients } from '../../public/components/login/login'
    @autoinject
    export class App {
        public router: Router;

        private TOKEN_KEY = "session";

        configureRouter(config: RouterConfiguration, router: Router): void {
            this.router = router;
            config.title = "Aurelia";
            config.addAuthorizeStep(AuthorizeStep);

            config.map([
                {
                    route: ["", "scheduler"],
                    name: "scheduler",
                    settings: {
                        icon: "scheduler",
                        auth: true,
                        roles: ["Employee", "Admin"]
                    },
                    moduleId: PLATFORM.moduleName("../components/scheduler/scheduler"),
                    nav: true,
                    title: "scheduler"
                },
                {
                    route: "clients",
                    name: "clients",
                    moduleId: PLATFORM.moduleName(
                        "../components/clients/clientList/clientList"
                    ),
                    title: "Clients",
                    nav: true,
                    settings: {
                        nav: [
                            { href: "#clients/clientsList", title: "Client List" },
                            { href: "#clients/Create", title: "Create Client" }
                        ],
                        auth: true,
                        roles: ["Employee", "Admin"],
                        pos: "left"
                    }
                },
                {
                    route: "clients/ClientsList",
                    name: "clientList",
                    moduleId: PLATFORM.moduleName(
                        "../components/clients/clientList/clientList"
                    ),
                    settings: {
                        auth: true,
                        roles: ["Employee", "Admin"]
                    }
                },
                {
                    route: "clients/create",
                    name: "aboutTeam",
                    moduleId: PLATFORM.moduleName(
                        "../components/clients/clientCreate/clientCreate"
                    ),
                    settings: {
                        auth: true,
                        roles: ["Employee", "Admin"]
                    }
                },
                {
                    route: "logout",
                    name: "logout",
                    settings: {
                        icon: "user",
                        auth: true,
                        roles: ["Employee", "Admin"],
                        pos: "right"
                    },
                    moduleId: PLATFORM.moduleName("../components/auth/logout/logout"),
                    nav: true,
                    title: "Logout"
                },
                {
                    route: "not-found",
                    name: "not-found",
                    settings: {
                        auth: true,
                        roles: ["Employee", "Admin"]
                    },
                    moduleId: PLATFORM.moduleName("../components/notFound/notFound"),
                    nav: false,
                    title: "Not Found"
                },
                {
                    route: "login",
                    name: "login",
                    settings: {
                        icon: "user",
                        auth: true,
                        roles: ["Employee", "Admin"],
                        pos: "right"
                    },
                    moduleId: PLATFORM.moduleName("../../public/components/login/login"),
                    nav: true,
                    title: "login"
                }
            ]);

            config.mapUnknownRoutes("not-found");
        }
    }

    @autoinject
    class AuthorizeStep {
        private endDate: any;
        static loginFragment = '../../public/components/login/login';

        constructor(
            private authService: AuthService,
            private router: Router,
            private aurelia: Aurelia
        ) { }


        run(navigationInstruction: NavigationInstruction, next: Next): Promise<any> {
            return Promise.resolve()
                .then(() => this.checkAuthentication(navigationInstruction, next))
                .then(result => result || this.checkAuthorization(navigationInstruction, next))
                .then(result => result || this.checkOrigin(navigationInstruction, next))
                    .then(result => result || next());
        }

        checkAuthentication(navigationInstruction, next) {
            // Do we have a JWT?
            const session = this.authService.getIdentity();
            if (!session) {
                this.forceReturnToPublic(next); // No JWT - back to the public root.
            }
            console.log("CHECKaUTHENTICATION: ", navigationInstruction.getAllInstructions().some(i => i.config.settings.auth) )
            if (navigationInstruction.getAllInstructions().some(i => i.config.settings.auth)) {
                // Is the token valid?
                if (this.authService.hasTokenExpired(session)) {
                    const currentUrl = navigationInstruction.fragment + (navigationInstruction.queryString ? `?${navigationInstruction.queryString}` : '');
                    console.log("FRAGMENT: ", navigationInstruction.fragment);
                    console.log("NAVIGATE INSTRUCTION: ", navigationInstruction)
                    console.log('currentURL: ', currentUrl);

                    localStorage.setItem('origin', currentUrl);
                    console.log("AuthorizeStep.loginFragment", AuthorizeStep.loginFragment)
                    next.cancel();
                    console.log("and it gets here!");
                    return new Redirect('login');
                }
            }

        }

        checkAuthorization(navigationInstruction, next) {
            var usersRole = this.authService.getUserRole();

            let requiredRoles = navigationInstruction.getAllInstructions()
                .map(i => i.config.settings.roles)[0];

            console.log("route Roles: ", requiredRoles);

            let isUserPermited = requiredRoles ? requiredRoles.some(r => r === usersRole) : true;

            console.log("isUserPermited: ", isUserPermited);

            if (!isUserPermited) {
                this.forceReturnToPublic(next);
            }

        }

        checkOrigin(instruction, next) {
            const origin = localStorage.getItem('origin');
            // Check if we were not redirected to login page and have an origin
            if (instruction.fragment !== AuthorizeStep.loginFragment && origin) {
                localStorage.removeItem('origin');
                return next.cancel(new Redirect(origin));
            }
        }

        forceReturnToPublic(next) {
            if (localStorage.getItem('origin')) {
                localStorage.removeItem('origin')  // Just in case we had origin set.
            }
            next.cancel();
            this.authService.clearIdentity();
            this.router.navigate("/", { replace: true, trigger: false });
            this.router.reset();
            this.aurelia.setRoot("public/public/public");
        }
    }

1 个答案:

答案 0 :(得分:1)

在您使用.slice的所有其他管道步骤中,它应该是相同的情况,因为管道步骤需要parseFloat(data.median_price.replace('$', '')); 作为返回值,但您返回return next.cancel(new Redirect())这里。

尝试将其更改为

Next