lockit-utils

restrict([config])

Prevent users who aren't logged-in from accessing routes. Use login.route for redirection. Function also remembers the requested url and user is redirected after successful login. If rest is enabled you'll get a 401 response.

Example

config.js

exports.login = {
  route: '/login'
};

app.js

var config = require('./config.js');
app.get('/private', utils.restrict(config), function(req, res) {
  res.send('only a logged in user can see this');
})

getDatabase(config)

Get type of database and database adapter name from connection information.

Returns

Example

config.js (CouchDB)

exports.db = 'http://127.0.0.1:5984/';

config.js (all other DBs)

exports.db = {
  url: 'postgres://127.0.0.1:5432/',
  name: 'users',
  collection: 'my_user_table'
}

app.js

var config = require('./config.js');
var db = util.getDatabase(config);
// {
//   type: 'couchdb',
//   adapter: 'lockit-couchdb-adapter'
// }

qr(config)

Generate link to QR code, uses Google Charts.

Returns

Example

var config = {
  key: 'abcd1234',
  email: 'mirco.zeiss@gmail.com'
};
var link = util.qr(config);
// https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FLockit%3Amirco.zeiss%40gmail.com%3Fsecret%3DMFRGGZBRGI2DI%3D%3D%3D%26issuer%3DLockit

verify(token, key, [options])

Verify a two-factor authentication token, uses time-based one-time password algorithm (totp). To be used with Google Authenticator.

Returns

Example

var key = 'abcd1234';
var token = '236709';
var valid = util.verify(token, key);
if (valid) {
  // continue here
}

destroy(req, done)

Destroy the current session. Works with cookie sessions and session stores.

Example

util.destroy(req, function() {
  // user is now logged out
});

pipe(source, target)

Pipe events from source to target. source can be a single event emitter or an Array of event emitters.

Example

var util = require('util');
var events = require('events');
var utils = require('lockit-utils');

var Child = function() {};
util.inherits(Child, events.EventEmitter);

var Mother = function() {};
util.inherits(Mother, events.EventEmitter);

var child = new Child();
var mother = new Mother();

utils.pipe(child, mother);

mother.on('action', function(action) {
  console.log('look the child is ' + action);
});

child.emit('action', 'smiling');