Implement the account interface using a couchdb backend with the couch-profile module
Install the module by executing
npm install -S account-couch
You will also need the views provided by the couch-profile module. You can add these to your couchdb database using the couchdb-update-views module.
# create a couchdb database named account_couch_test
curl -X PUT localhost:5984/account_couch_test
# install the couchdb-update-views module globally so the couchdb-update-views command is available on the command line
npm install -g couchdb-update-views
# add the couch-profile views to the newly created account_couch_test database
couchdb-update-views --config test/config.json --docsDir node_modules/couch-profile/docs
You can perform the steps listed above by executing the travis before_install script with the command
script/createDatabase.sh
script/createViews.sh
The account couch module implements and exports the following interface functions:
login
register
serializeUser
deserializeUser
removeUser
To register a new account, pass an object with an email
, and a password
var Account = require('account-couch')
var config = require('nconf').defaults({
couch: {
host: 'localhost',
port: 5984,
database: 'account_couch_test'
}
})
var db = require('cradle-nconf')(config)
// create the account object with the cradle db passed as a parameter to the constructor
var account = new Account(db)
var data = {
email: 'foo@example.com',
password: 'barPassword',
}
account.register(data, function (err, reply) {
if (err) {
inspect(err, 'error registering user account')
return
}
inspect(reply, 'user account created correctly'
})
To login an existing account, pass an object with an email
, and a password
fields set
var Account = require('account-couch')
var config = require('nconf').defaults({
couch: {
host: 'localhost',
port: 5984,
database: 'account_couch_test'
}
})
var db = require('cradle-nconf')(config)
// create the account object with the cradle db passed as a parameter to the constructor
var account = new Account(db)
var data = {
email: 'foo@example.com',
password: 'barPassword',
}
account.login(data, function (err, reply) {
if (err) {
inspect(err, 'error performing login for user account')
return
}
inspect(reply, 'user account created correctly'
})
To serialize an existing user account profile, pass a profile object to the serializeUser method. You will get back the _id
of the profile in the couchdb database
var Account = require('account-couch')
var config = require('nconf').defaults({
couch: {
host: 'localhost',
port: 5984,
database: 'account_couch_test'
}
})
var db = require('cradle-nconf')(config)
// create the account object with the cradle db passed as a parameter to the constructor
var account = new Account(db)
var data = {
email: 'foo@example.com',
password: 'barPassword',
}
// get the profile by logging in
account.login(data, function (err, profile) {
if (err) {
inspect(err, 'error performing login for user account')
return
}
inspect(profile, 'user account profile')
account.serializeUser(profile, function (err, reply) {
if (err) {
inspect(err, 'error performing login for user account')
return
}
inspect(reply, 'profile serialized to _id')
})
})
To deserialize an existing user account profile, pass an _id
of the profile in the couchdb database
var Account = require('account-couch')
var config = require('nconf').defaults({
couch: {
host: 'localhost',
port: 5984,
database: 'account_couch_test'
}
})
var db = require('cradle-nconf')(config)
// create the account object with the cradle db passed as a parameter to the constructor
var account = new Account(db)
// an _id of a user profile in the couchdb database
var id = 'fooID'
account.deserializeUser(id, function (err, profile) {
if (err) {
inspect(err, 'error performing login for user account')
return
}
inspect(profile, 'user account profile')
})
To completly remove an existing user account profile from the couchdb database, pass an object containing the email
and password
of the user account profile to be removed from the couchdb database
var Account = require('account-couch')
var config = require('nconf').defaults({
couch: {
host: 'localhost',
port: 5984,
database: 'account_couch_test'
}
})
var db = require('cradle-nconf')(config)
// create the account object with the cradle db passed as a parameter to the constructor
var account = new Account(db)
var data = {
email: 'foo@example.com',
password: 'barPassword',
}
account.removeUser(data, function (err) {
if (err) {
inspect(err, 'error removing profile')
return
}
inspect('user account profile removed')
})
To run the test suite execute
# install development dependencies
npm install
# run tests
npm test