question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How pass params to the Middleware?

See original GitHub issue

Congratulations, it’s a cool framework.

I’m need restrict access to route on role-based. This works for me on express:

app.get('/dashboard', middleware.hasRole(['admin', 'creator', 'editor']), function(req,res){});

I have tried to implement a NestMiddleware, but I can not pass the parameters to it:

builder.use({
    middlewares: [ HasRole ],
    forRoutes: [{
    	path: 'dashboard', 
    	method: RequestMethod.ALL, 
    	data: { 'roles': ['admin', 'creator', 'editor'] } 
    }]
})

Here we can think of implementing guardians directly in the controller, inspired by the canActivate concept of Angular2:

{
  path: 'dashboard', 
  component: dashboardComponent, 
  canActivate: [ RoleGuard ], 
  data: { roles: [ 'admin', 'creator', 'editor' ] } 
}

Something like so:

@RequestMapping({ 
	path: '/dashboard', 
	method: RequestMethod.GET, 
	canActivate: [ HasRole ],
	data: { roles: [ 'admin', 'creator', 'editor' ] } 
})

@kamilmysliwiec What do you think is better, do you plan to implement something like this for the future?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kamilmysliwieccommented, Apr 11, 2017

So finally:

Since RC.8 - use() method is deprecated. Instead of use() method you should use apply().

Old way (deprecated):

builder.use({
    middlewares: [ HasRole ],
    forRoutes: [{
    	path: 'dashboard', 
    	method: RequestMethod.ALL, 
    	data: { 'roles': ['admin', 'creator', 'editor'] } 
    }]
})

New way:

builder.apply(HasRole)
    .with('admin', 'creator', 'editor')
    .forRoutes({
        path: 'dashboard',
        method: RequestMethod.ALL
    });

Using with() you can pass whatever you want (objects, arrays etc.) to the middleware resolve(...args) method. You only have to separate arguments by comma.

Now, your resolve() method of HasRole middleware should looks like that:

class HasRole {
    resolve(...roles) {
        /// the rest
    }
}

Where roles is an array of arguments passed by with() method (in this example, I used spread operator).

0reactions
lock[bot]commented, Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Pass Parameters with Express Middleware
Hey all, Today we will be talking about how you can pass parameters in the Express middleware. We will be explicitly talking about...
Read more >
javascript - Passing variables to the next middleware using ...
The most common pattern for passing variables on to other middleware and endpoint functions is attaching values to the request object req ....
Read more >
Passing Parameters To Middleware In ASP.NET Core 2.0
How do you pass parameters to middleware during its setup in ASP.NET Core? Solution. In an empty project add a POCO class to...
Read more >
How to pass variables to the next middleware using next() in ...
The following example covers how to pass variables to the next middleware using next() in Express.js. Approach: We cannot directly pass data ...
Read more >
Pass parameters to middleware from Controller - Laracasts
Hello I have been searching about this topic and I found some answers but nothing solid, so here we are Im developing a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found