Comparing version 3.0.8 to 3.0.9
48
nano.js
@@ -42,2 +42,3 @@ /* minimal couch in node | ||
* dinosaurs spaceships! | ||
* | ||
*/ | ||
@@ -55,4 +56,4 @@ module.exports = exports = nano = function database_module(cfg) { | ||
/**************************************************************************** | ||
* relax * | ||
/*************************************************************************** | ||
* relax * | ||
***************************************************************************/ | ||
@@ -310,5 +311,36 @@ /* | ||
/**************************************************************************** | ||
* db * | ||
/*************************************************************************** | ||
* auth * | ||
***************************************************************************/ | ||
/* | ||
* gets a session going on for you | ||
* | ||
* e.g. | ||
* nano.auth(username, password, function (err, body, headers) { | ||
* if (err) { | ||
* return console.log("oh noes!") | ||
* } | ||
* | ||
* if (headers && headers['set-cookie']) { | ||
* console.log("cookie monster likes " + headers['set-cookie']); | ||
* } | ||
* }); | ||
* | ||
* @param {username:string} username | ||
* @param {password:string} password | ||
* | ||
* @see relax | ||
*/ | ||
function auth_server(username, password, callback) { | ||
return relax( | ||
{ method : "POST" | ||
, db : "_session" | ||
, form : { "name" : username, "password" : password } | ||
, content_type : "application/x-www-form-urlencoded; charset=utf-8" | ||
}, callback); | ||
} | ||
/*************************************************************************** | ||
* db * | ||
***************************************************************************/ | ||
/* | ||
@@ -795,2 +827,3 @@ * creates a couchdb database | ||
} | ||
, auth : auth_server // alias | ||
, insert : insert_doc | ||
@@ -810,3 +843,3 @@ , get : get_doc | ||
, atomic : update_with_handler_doc | ||
, updateWithHandler : update_with_handler_doc // alias | ||
, updateWithHandler : update_with_handler_doc // alias | ||
}; | ||
@@ -830,3 +863,3 @@ | ||
, use : document_module // alias | ||
, scope : document_module // alias | ||
, scope : document_module // alias | ||
, compact : compact_db | ||
@@ -841,3 +874,4 @@ , replicate : replicate_db | ||
, relax : relax // alias | ||
, dinosaur : relax // alias | ||
, dinosaur : relax // alias | ||
, auth : auth_server | ||
}; | ||
@@ -844,0 +878,0 @@ |
@@ -5,3 +5,3 @@ { "name" : "nano" | ||
, "repository" : "git://github.com/dscape/nano" | ||
, "version" : "3.0.8" | ||
, "version" : "3.0.9" | ||
, "author" : "Nuno Job <nunojobpinto@gmail.com> (http://nunojob.com)" | ||
@@ -8,0 +8,0 @@ , "contributors" : |
@@ -458,60 +458,46 @@ # nano | ||
login... | ||
``` js | ||
var nano = require('nano')('http://localhost:5984'), | ||
username = 'user', // your user's credentials (passed in from a form on your site for example) | ||
userpass = 'pass'; | ||
var nano = require('nano')('http://localhost:5984') | ||
, username = 'user' | ||
, userpass = 'pass' | ||
, callback = console.log // this would normally be some callback | ||
, cookies = {} // store cookies, normally redis or something | ||
; | ||
nano.auth(username, userpass, function (err, body, headers) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
nano.request({ | ||
method: "POST", | ||
db: "_session", | ||
form: { name: username, password: userpass }, | ||
content_type: "application/x-www-form-urlencoded; charset=utf-8" | ||
}, | ||
function (err, body, headers) { | ||
if (err) { res.send(err.reason); return; } | ||
if (headers && headers['set-cookie']) { | ||
cookies[user] = headers['set-cookie']; | ||
} | ||
// send couchdb's cookie right on through to the client | ||
if (headers && headers['set-cookie']) { | ||
res.cookie(headers['set-cookie']); | ||
} | ||
res.send('logged in!'); | ||
}); | ||
callback(null, "It worked"); | ||
}); | ||
``` | ||
... perform tasks using cookie authentication ... | ||
reusing a cookie: | ||
``` js | ||
var auth = req.cookies['AuthSession'], | ||
nano; | ||
var auth = "some stored cookie" | ||
, callback = console.log // this would normally be some callback | ||
, nano require('nano')( | ||
{ url : 'http://localhost:5984/alice', cookie: 'AuthSession=' + auth }); | ||
; | ||
if (!auth) { res.send(401); return; } | ||
// set-up nano with cookie authentication | ||
nano = require('nano')({ url : 'http://localhost:5984', cookie: 'AuthSession=' + auth }); | ||
alice.insert(doc, function (err, body, headers) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var alice = nano.use('alice'); | ||
// change the cookie if couchdb tells us too | ||
if (headers && headers['set-cookie']) { | ||
auth = headers['set-cookie']; | ||
} | ||
alice.insert(doc, null, | ||
function (err, body, headers) { | ||
if (err) { res.send(err.reason); return; } | ||
// update the cookie held in the browser, if couchdb has sent an updated version | ||
if (headers && headers['set-cookie']) { res.cookie(headers['set-cookie']); } | ||
res.send('ok'); | ||
} | ||
); | ||
callback(null, "It worked"); | ||
}); | ||
``` | ||
... and finally, logout ... | ||
``` js | ||
// the couchdb cookie name is AuthSession | ||
res.clearCookie('AuthSession'); | ||
res.send('logged out!'); | ||
``` | ||
## advanced features | ||
@@ -518,0 +504,0 @@ |
@@ -27,8 +27,4 @@ var specify = require("specify") | ||
// authenticate | ||
nano.relax( | ||
{ method : "POST" | ||
, db : "_session" | ||
, form : { "name" : "admin", "password" : "password" } | ||
, content_type : "application/x-www-form-urlencoded; charset=utf-8" | ||
}, function (err, response, headers) { | ||
nano.auth(helpers.username, helpers.password, | ||
function (err, response, headers) { | ||
assert.equal(err, undefined, "Should have logged in successfully"); | ||
@@ -35,0 +31,0 @@ assert.ok(headers['set-cookie'], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132719
3186
633