manage accounts with leveldb
To create a user, we can just do:
var accountdown = require('accountdown');
var level = require('level');
var db = level('/tmp/users.db');
var users = accountdown(db, {
login: { basic: require('accountdown-basic') }
});
var opts = {
login: { basic: { username: 'substack', password: 'beep boop' } },
value: { bio: 'beep boop' }
};
users.create('substack', opts, function (err) {
if (err) console.error(err);
});
The string we gave to users.create()
need not necessarily match the username
credential. You might want to use a uid
integer for example. In either case
you will get implicitly encforced unique names because if a username already
exists the users.create()
call will fail even if the id
is available and
likewise if an id
is unavailable but a username is available.
To verify a credential:
var accountdown = require('accountdown');
var level = require('level');
var db = level('/tmp/users.db');
var users = accountdown(db, {
login: { basic: require('accountdown-basic') }
});
var creds = { username: 'substack', password: 'beep boop' };
users.verify('basic', creds, function (err, ok) {
if (err) console.error(err)
else console.log('verified:', ok)
});
var accountdown = require('accountdown')
Create a new account instance users
given a leveldb database handle db
and
some options opts
.
opts
can be:
opts.login
- map of type names to login plugin functionsopts.valueEncoding
- value encoding to use, 'json' by default
Create a user by an id
.
Get the data for a username
Register a login plugin
at a string name type
.
Login plugins such as
accountdown-basic
should export a single function that will be created with db
and prefix
arguments and should return an object with .verify()
and .create()
functions:
module.exports = function (db, prefix) {
return {
verify: function (creds, cb) {
// calls cb(err, success, id)
},
create: function (id, creds) {
// returns an array of batch rows
}
};
};
Check whether creds
are valid login credentials.
cb(err, success, id)
fires with any errors, the success as a boolean, and the
user id
.
Return an array of rows to batch insert if and only if all the keys do not already exist using level-create-batch.