![Build Status](https://travis-ci.org/iofjuupasli/guard.svg?branch=master)
guard
Utility to split opportunities by the account level (free\pro\enterprise\etc.) for frontend and node
Install
With bower
bower i -S guard
With npm
npm i -S guard-bit
File
Save guard.min.js or guard.js to your assets folder
Examples
Without any config
var guard = Guard();
guard();
guard('anyRule');
guard(function () {
console.log('called!');
})();
guard('anyRule', function () {
console.log('called!');
})();
guard.setLevel(1);
guard();
guard('anyRule');
guard(function () {
console.log('called!');
})();
guard('anyRule', function () {
console.log('called!');
})();
Request callback (unauth/auth)
var guard = Guard(function (done) {
setTimeout(function () {
if (confirm('Give access?')) {
done();
} else {
done('error');
}
}, 100);
});
guard();
guard('anyRule');
guard(function (message) {
console.log(message);
})('called!');
guard('anyRule', function () {
console.log('called!');
})();
guard();
guard('anyRule');
guard(function () {
console.log('called!');
})();
guard('anyRule', function () {
console.log('called!');
})();
Several access level and rules (unauth/auth/pro/admin)
var guard = Guard([{
allowed: ['profile:create'],
request: function (done) {
if (confirm('Are you a User?')) {
done();
} else {
done('error');
}
}
}, {
allowed: ['@:profile:*'],
request: function (done) {
if (confirm('Are you an Admin?')) {
done();
} else {
done('error');
}
}
}, {
allowed: ['*']
}]);
guard();
guard('profile:create');
guard(function (message) {
console.log(message);
})('called!');
guard('@:profile:read', function () {
console.log('My Profile');
})();
guard('adminFeature', function () {
console.log('called!');
})();
guard.setLevel(0);
guard('adminFeature', function (laugh) {
console.log(laugh + ' I\'m an Admin!');
})('Ha!');
As middleware
function guardMiddleware(rule) {
return function (req, res, next) {
if (guard(rule)) {
next();
} else {
res.sendStatus(401);
}
}
}
app.get('/profile', guardMiddleware('@:profile:read'),
function (req, res) {
res.send('Your profile');
});
More useful methods
var guard = Guard(function (done) {
console.log('Request for Auth')
done();
});
guard.listen(function (newLevel) {
console.log('now on level ' + newLevel);
});
var unsubscribe = guard.listen(function (newLevel) {
console.log('second listener!');
});
guard.request();
unsubscribe();
guard.getLevel();
guard.setLevel(0);
guard.request(function () {
console.log('after success uplevel');
});
guard.setup(config);
API (TypeScript)
interface Guard {
() : boolean;
(feature : string) : boolean;
(callback : (...any) => any) : (...any) => void;
(feature : string, callback : (...any) => any) : (...any) => void;
request() : void;
request(callback : (...any) => any);
setup() : void;
setup(req: (done: (error? : any) => any) => any) : void;
setup(config: Array<{request?: (done: (error? : any) => any) => any; allowed: Array<any>}>) : void;
getLevel() : number;
setLevel(level : number) : void;
listen(listener : (newLevel) => any) : () => void;
}
interface GuardConstructor {
() : Guard;
new () : Guard;
(req: (done: (error? : any) => any) => any) : Guard;
new (req: (done: (error? : any) => any) => any) : Guard;
(config: Array<{request?: (done: (error? : any) => any) => any; allowed: Array<any>}>) : Guard;
new (config: Array<{request?: (done: (error? : any) => any) => any; allowed: Array<any>}>) : Guard;
}
declare module "guard" {
export = Guard;
}
declare var Guard : GuardConstructor;
Versions
It uses semver.
Current version is 1.x.x. It means that API is stable. Whooa!