Comments (4)
Thanks your suggestion but I think this (your example) is a bad use case of koa middleware.
- It creates and converts koa middleware for every requests, which is unnecessary.
- It's error-prone. There might be a bug in your example code.
In rbac.allow
at https://github.com/yanickrochon/koa-rbac/blob/master/rbac.js#L115, it restricts permission by return
and not calling yield next
. In your example, next
is just noop
and user without permission will continue go through downstream.
from convert.
You are right - I forgot to update this snippet when I encountered that bug. Here is how i'm handling this now:
export async function organizationIdParam (id, ctx, next) {
await convert(rbac.allow('read:organization', { organization: id }))(ctx, noop);
if (ctx.status === 403) return;
ctx.organization = await Organization.findById(id);
await next();
}
from convert.
I would not recommend you to create middleware dynamically like this. It seems that your use case (inject id dynamically) has beyond the scope of that middleware. (I am not familiar with that middleware and I might be wrong).
Also, ctx.status === 403
is hacking. I would suggest you to do it in this way:
export function organizationIdParam (id, ctx, next) { // or `async function` if you want
return convert.compose(
rbac.allow('read:organization', { organization: id }),
async function (ctx, next) {
ctx.organization = await Organization.findById(id);
await next();
}
)(ctx, next);
}
from convert.
Great, that makes sense. Thank you!
from convert.
Related Issues (8)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from convert.