tenodi / permission Goto Github PK
View Code? Open in Web Editor NEWNpm package for hangling user permissions for routes based on roles.
Home Page: https://www.npmjs.com/package/permission
License: MIT License
Npm package for hangling user permissions for routes based on roles.
Home Page: https://www.npmjs.com/package/permission
License: MIT License
how can I handle redirect at the control level? example if you go to url/admin take them back to sign in page if not an admin.
Hi there!
I using PassportJS to authenticate users on my site.
My routes looks like:
/routes/songs.js:
router.route('/')
.get(Songs.renderExplore)
.post(require('permission')(['admin']), upload.array(), Songs.create)
this is the only place where I use permission.
Also I created a field named 'role' in my User schema.
Then I debugged my app and observe that at this point:
if (req.isAuthenticated() && !req.user[role])
throw new Error("User doesn't have property named: " + role + ". See Advantage Start in docs")
req.user is undefined
but console.log(req.session.passport) returns me:
user: '57338a9a5c52e9159fc89095'
it mean that user is authorized and there is a record in session, but req.user is undefined;
my suggestion is that permission checks req.user before it actually filled by passport.deserializeUser()
How do you use permission with Passportjs?
Thanks in advance!
add the ability to add a parameter to a user to be a superuser that will have access to everything
Hi,
Thank you for creating this great package and making my coding life much easier.
ISSUE
The source code currently in NPM is not the same as the one in this repo. Both versions say 1.0.4 but the code in NPM does not support user.role as an array while the code in the GitHub Repo does.
WORKAROUND
I was able to work around this issue temporarily by doing the following:
npm install https://github.com/tenodi/permission
Thanks!
What about something like this in index.js? (This is not the most elegant algorithm nor did I run it yet. It's just an idea at this point.)
if (req.isAuthenticated()) {
if (!roles || roles.indexOf(req.user[role]) > -1){
after(req, res, next, permission.AUTHORIZED);
} else if (req.user[role].hasProperty('forEach)) {
var perm = permission.NOT_AUTHORIZED;
req.user[role].forEach(function(userRole, i){
if(!roles || roles.indexOf(userRole) > -1) perm = permission.AUTHORIZED;
}
after(req, res, next, perm);
} else {
after(req, res, next, permission.NOT_AUTHORIZED);
}
}
else {
after(req, res, next, permission.NOT_AUTHENTICATED);
}
I have a config file with different roles&permissions array that can be quickly changed. Many arrays are empty and they gent sent to permissions. Permissions treats it as a normal list of permissions and lets no user pass
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.