access-manager
A one-stop solution for implementing authenticated and anonymous sessions with user handling and whitelisted ACL. Attaches itself to an express app as a middleware.
Install
$ npm install access-manager
Install ACL data
Use the switch when you start your app with access manager for the first time. (Note that your app will shut down once the import is done.)
Use example data: (example-acl.json)
$ node app --import-acl
Or provide your own file:
$ node app --import-acl=file.json
Examples:
const AccessManager = require('access-manager');
const accessManager = new AccessManager({
mongoose: mongoose,
expressApp: app
});
You can optionally add your own schemas (Schema Objects) for users, sessions and acl, at the properties: userSchema, sessionSchema, aclSchema
Some properties in the schemas are required by the access-manager. Those details can be found at the bottom of this document.
You will propably want to supply your own userSchema, an example of doing that:
const accessManager = new AccessManager({
mongoose: mongoose,
expressApp: app,
userSchema: {
firstName: {type: String, required:true},
lastName: {type: String, required:true},
email: {type: String, required:true, unique:true},
password: {type: String, required:true},
roles: [String]
}
const User = accessManager.models.user;
app.post('/register', async (req, res)=>{
let user = await new User(req.body);
await user.save();
res.json({msg:'Registered'});
});
app.post('/login', async (req, res)=>{
if(passwordsMatch){
req.session.user = user._id;
req.session.loggedIn = true;
await req.session.save();
res.json({msg:'Logged in'});
}else{
res.json({msg:'Failed login'});
}
});
app.all('/logout', async (req, res)=>{
req.user = {};
req.session.loggedIn = false;
await req.session.save();
res.json({msg:'Logged out'});
});
app.get('/messages', async (req, res)=>{
res.json({msg:'Here are your messages'});
});
Access manager schemas requirements
The schemas used in access manager must contain the properties detailed below. (If you don't supply your own schemas these are the defaults)
The userSchema must have the properties:
"email" (string),
"password" (string)
"roles" (array of strings)
The sessionSchema must have the properties:
"loggedIn" (bool)
"user" (reference)
The aclSchema must have the properties:
"path" (string)
"roles" (array of child schema containing):
"role (string)
"methods (array of string with enum: ['GET', 'POST', 'PUT', 'DELETE', 'ALL'])