Aurelia: During a Router's Pipeline Step, how do I bind a variable to that router?

后端 未结 2 1528
清酒与你
清酒与你 2020-12-18 01:58

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

2条回答
  •  一生所求
    2020-12-18 02:33

    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();
        }
    }
    

提交回复
热议问题