@kos-ng-it-konsult/auth-manager
This is an auth management package for KOS NG IT Konsult.
Quick Start
Install
$ npm i --save @kos-ng-it-konsult/auth-manager
Basic Usage
"use strict";
const express = require('express'); // run npm install --save express
const bodyparser = require('body-parser'); // run npm install --save body-parser
const http = require('http');
const lib = require('../index');
const options = {
header_session_token: 'x-session-token',
header_access_token: 'x-access-token',
permissions_key: 'user_permissions',
cache_key: 'user-sessions'
};
const auth_manager = new lib(..., options,);
const app = express();
app.use(bodyparser.urlencoded({ extended: true, limit: '100mb' }));
app.use(bodyparser.json({ limit: '100mb' }));
function endRequest () {
return (request, response, next) => {
return response.status(200).send({
status: 'success',
message: 'thank you'
});
};
}
app.route('/test')
.get(auth_manager.authenticate(), endRequest())
.post(auth_manager.authenticate(), auth_manager.authorize('create_test'), endRequest()) // Authenticate user and then authorize user action
.put(auth_manager.authenticate(), auth_manager.authorize({ and: [ 'create_test', 'update_test' ] }), endRequest()); // Authenticate user and then authorize user action
app.route('/test/:user_id')
.get(auth_manager.authenticate(false), endRequest()) // Set strict authentication to false
.put(validator.validateRequest(), endRequest());
const server = http.Server(app);
server.listen((process.env.PORT || 3005), (error) => {
if (!error) {
console.log(`server is listening on port ${ (process.env.PORT || 3005) }`);
} else {
console.log(`Error starting server: ${ error.toString() }`);
}
});
app.use((error, request, response, next) => {
response.status((error.statusCode || 500)).send({
status: 'error',
message: (error.message || 'Internal server error, please try again')
});
});
Running the Example
To run the example, first install express
and body-parser
using
$ npm install --save express
$ npm install --save body-parser
Then run the sample command
$ node ./example/sample.js
Running Tests
Run
$ npm test