Aurelia: During a Router's Pipeline Step, how

2019-01-25 16:11发布

I'd like to pass the user, found during the AuthorizeStep to either the App class and then to the home module.

Here's what I have:

export class App {
    configureRouter(config, router) {
        config.addPipelineStep('authorize', AuthorizeStep); 
        config.map([
            {route: ['', ':filter'], name: "", moduleId: 'welcome'}
            {route: 'home', name: "home", moduleId: 'home' auth:true}
        ]);
        this.router = router;
    }
}

class AuthorizeStep {
    run(routingContext, next) {
        if (routingContext.nextInstructions.some(i => i.config.auth)) {
            this.client.get('auth/login')
                .then(response => {
                    this.user = response.content;
                });
        }
        return next();
    }
}

2条回答
姐就是有狂的资本
2楼-- · 2019-01-25 16:37

I have been doing something similar, but I found that I can't rely on the authcontext being populated in other viewmodels by the time the viewmodel is being attached. Returning the promise returned by the get and then returning next() within the resolution of the get seems to solve that, the idea being to not proceed to the next pipeline step until this one has resolved. Applying that to the answer from @JamesCarters, I'd get the following (untested) code:

class AuthorizeStep {
    static inject() { return [AuthContext];}

    constructor(authcontext){
        this.authContext = authcontext;
    }
    run(routingContext, next) {
        if (routingContext.nextInstructions.some(i => i.config.auth)) {
            return this.client.get('auth/login')
                .then(response => {
                    this.authcontext.user = response.content;
                    return next();
                });
        }
        else {
            return next();
        }
    }
}
查看更多
forever°为你锁心
3楼-- · 2019-01-25 16:51

In my app I created a class called AuthContext with currentUser property. You can inject it in the constructor for the AuthorizeStep and then inject it in any other models that need it. Something like...

import {AuthContext} from './auth-context';

export class App {
    static inject() { return [AuthContext];}

    constructor(authcontext){
        this.authContext = authcontext;
    }

    configureRouter(config, router) {
         config.addPipelineStep('authorize', AuthorizeStep); 
         config.map([
            {route: ['', ':filter'], name: "", moduleId: 'welcome'}
            {route: 'home', name: "home", moduleId: 'home' auth:true}
        ]);
        this.router = router;
    }
}

class AuthorizeStep {
    static inject() { return [AuthContext];}

    constructor(authcontext){
        this.authContext = authcontext;
    }
    run(routingContext, next) {
        if (routingContext.nextInstructions.some(i => i.config.auth)) {
            this.client.get('auth/login')
                .then(response => {
                    this.authcontext.user = response.content;
                });
        }
        return next();
    }
}
查看更多
登录 后发表回答