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();
}
}
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 theget
and then returningnext()
within the resolution of theget
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: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...