SNO
SNO

Reputation: 866

Aurelia - Request web service in pipeline step

I’m trying to setup a pipeline step “role management” where I need to request the web service. Now my problem is, that the http-request is asynchonious and so the redirect is never triggered correctly.

 run(routingContext, next){
        if (routingContext.getAllInstructions().some(i => i.config.permission)) {

            let permission = routingContext.getAllInstructions()[0].config.permission;
            this.roleService.userIsAllowedTo(permission)
                .then(boolResponse => {
                    if(boolResponse){
                        return next();
                    }else{
                        return next.cancel(new Redirect("/"));
                    }
                });
        }
        return next();
    }

Can someone show me how to solve that?

Upvotes: 0

Views: 89

Answers (2)

SNO
SNO

Reputation: 866

Thank you very much. I could solve that Issue now by changing the method to an async-method:

async run(routingContext, next){
        if (routingContext.getAllInstructions().some(i => i.config.permission)) {

            let permission = routingContext.getAllInstructions()[0].config.permission;
            let isallowed  = await this.roleService.userIsAllowedTo(permission);
            if(isallowed){
                return next();
            }else{
                return next.cancel();
            }
        }
        return next();
    }

Upvotes: 1

valu
valu

Reputation: 1196

Just return Promise from run()

    return this.roleService.userIsAllowedTo(permission).then(boolResponse => {
                    if(boolResponse){
                        return next();
                    }else{
                        return next.cancel(new Redirect("/"));
                    }
                });

Upvotes: 1

Related Questions