couch-login
This module lets you log into couchdb to get a session token, then make
requests using that session. It is basically just a thin wrapper around
@mikeal's request module.
This is handy if you want a user to take actions in a couchdb database
on behalf of a user, without having to store their couchdb username and
password anywhere. (You do need to store the AuthSession token
somewhere, though.)
Usage
var CouchLogin = require('couch-login')
http.createServer(function (req, res) {
var couch = new CouchLogin('http://my-couch.iriscouch.com:5984/')
if (sessionToken) {
req.couch.token = sessionToken
req.couch.get('/_session', function (er, resp, data) {
if (er || resp.statusCode !== 200) {
res.statusCode = resp.statusCode || 403
return res.end('Invalid login or something')
}
req.couch.get('/_users/org.couchdb.user:' + data.userCtx.name, etc)
})
} else {
var login = { name: name, password: password }
req.couch.login(login, function (er, resp, data) {
if (er || resp.statusCode !== 200) {
res.statusCode = resp.statusCode || 403
return res.end('Invalid login or something')
}
res.statusCode = 200
res.write("Who's got two thumbs and just logged you into couch?\n")
setTimeout(function () {
res.end("THIS GUY!")
}, 500)
})
}
})
Class: CouchLogin
new CouchLogin(couchdbUrl)
Create a new CouchLogin object bound to the couchdb url.
In addition to these, the get
, post
, put
, and del
methods all
proxy to the associated method on request.
However, as you'll note in the example above, only the pathname portion
of the url is required. Urls will be appended to the couchdb url passed
into the constructor.
If you have to talk to more than one couchdb, then you'll need more than
one CouchLogin object, for somewhat obvious reasons.
All callbacks get called with the following arguments, which are exactly
identical to the arguments passed to a request
callback.
er
{Error | null} Set if a communication error happens.resp
{HTTP Response} The response from the request to couchdbdata
{Object} The parsed JSON data from couch
couch.token
An object representing the couchdb session token. (Basically just a
cookie and a timeout.)
If the token has already timed out, then setting it will have no effect.
couch.login(auth, callback)
auth
{Object} The login details
name
{String}password
{String}
callback
{Function}
When the callback is called, the couch.token
will already have been
set (assuming it worked!), so subsequent requests will be done as that
user.
couch.get(path, callback)
GET the supplied path from the couchdb using the credentials on the
token.
Fails if the token is invalid or expired.
couch.del(path, callback)
DELETE the supplied path from the couchdb using the credentials on the
token.
Fails if the token is invalid or expired.
couch.post(path, data, callback)
POST the data to the supplied path in the couchdb, using the credentials
on the token.
Fails if the token is invalid or expired.
couch.put(path, data, callback)
PUT the data to the supplied path in the couchdb, using the credentials
on the token.
Fails if the token is invalid or expired.
couch.changePass(newAuth, callback)
Must already be logged in. Updates the _users
document with new salt
and hash, and re-logs in with the new credentials. Callback is called
with the same arguments as login, or the first step of the process that
failed.
couchdb.logout(callback)
Delete the session out of couchdb. This makes the token permanently
invalid.