Skip to content

zemirco/lockit-couchdb-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lockit CouchDB adapter

Build Status NPM version

CouchDB adapter for Lockit.

Installation

npm install lockit-couchdb-adapter

var adapter = require('lockit-couchdb-adapter');

Configuration

The adapter automatically saves the necessary views to your CouchDB. You only need the connection string in your config.js.

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

or (long format with custom per-user-db prefix)

exports.db = {
  url: 'http://127.0.0.1:5984/',
  prefix: 'custom/'  // default is 'lockit/'
}

Features

1. Create user

adapter.save(name, email, pass, callback)

  • name: String - i.e. 'john'
  • email: String - i.e. '[email protected]'
  • pass: String - i.e. 'password123'
  • callback: Function - callback(err, user) where user is the new user now in our database.

The user object has the following properties

  • _id: unique id from CouchDB
  • _rev: revision from CouchDB
  • password_scheme: pbkdf2
  • iterations: pbkdf2 iterations
  • name: name chosen during sign up
  • email: email that was provided at the beginning
  • roles: Array with roles a user has, default is ['user']
  • type: document type for views, default is 'user'
  • signupToken: unique token sent to user's email for email verification
  • signupTimestamp: Date object to remember when the user signed up
  • signupTokenExpires: Date object usually 24h ahead of signupTimestamp
  • failedLoginAttempts: save failed login attempts during login process, default is 0
  • derived_key: password hash generated by pbkdf2
  • salt: salt generated by crypto.randomBytes()
adapter.save('john', '[email protected]', 'secret', function(err, user) {
  if (err) console.log(err);
  console.log(user);
  // {
  //  _id: '8c7cd00c55a25ceb279a8e893d011b3e',
  //  _rev: '1-530a4cd8e67d51daf74e059899c39cd5',
  //  password_scheme: 'pbkdf2',
  //  iterations: 10,
  //  name: 'john',
  //  email: '[email protected]',
  //  roles: [ 'user' ],
  //  type: 'user',
  //  signupToken: 'fed26ce9-2628-405a-b9fa-285d4a66f4c3',
  //  signupTimestamp: '2013-09-21T10:10:50.357Z',
  //  signupTokenExpires: '2014-01-15T15:27:29.020Z',
  //  failedLoginAttempts: 0,
  //  derived_key: '0a2b1714d6017e7efdc1154ee34c805dea29c06a',
  //  salt: '3a213c87b6a33c70fec767acea697994'
  // }
});

2. Find user

adapter.find(match, query, callback)

  • match: String - one of the following: 'name', 'email' or 'signupToken'
  • query: String - corresponds to match, i.e. '[email protected]'
  • callback: Function - callback(err, user)
adapter.find('name', 'john', function(err, user) {
  if (err) console.log(err);
  console.log(user);
  // {
  //  _id: '8c7cd00c55a25ceb279a8e893d011b3e',
  //  _rev: '1-530a4cd8e67d51daf74e059899c39cd5',
  //  password_scheme: 'pbkdf2',
  //  iterations: 10,
  //  name: 'john',
  //  email: '[email protected]',
  //  roles: [ 'user' ],
  //  type: 'user',
  //  signupToken: 'fed26ce9-2628-405a-b9fa-285d4a66f4c3',
  //  signupTimestamp: '2013-09-21T10:10:50.357Z',
  //  signupTokenExpires: '2014-01-15T15:27:29.020Z',
  //  failedLoginAttempts: 0,
  //  derived_key: '0a2b1714d6017e7efdc1154ee34c805dea29c06a',
  //  salt: '3a213c87b6a33c70fec767acea697994'
  // }
});

3. Update user

adapter.update(user, callback)

  • user: Object - must have _id and _rev properties
  • callback: Function - callback(err, user) - user is the updated user object
// get a user from db first
adapter.find('name', 'john', function(err, user) {
  if (err) console.log(err);

  // add some new properties to our existing user
  user.newKey = 'and some value';
  user.hasBeenUpdated = true;

  // save updated user to db
  adapter.update(user, function(err, user) {
    if (err) console.log(err);
    // ...
  });
});

4. Delete user

adapter.remove(name, callback)

  • name: String
  • callback: Function - callback(err, res) - res is true if everything went fine
adapter.remove('john', function(err, res) {
  if (err) console.log(err);
  console.log(res);
  // true
});

Test

grunt

License

Copyright (C) 2013 [Mirco Zeiss](mailto: [email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.