Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[cookies docs minor] minor changes like removing references to expres…

…s from docs
  • Loading branch information...
commit 5d328da2ed48c90d47474d43e96e0f5998f8321e 1 parent 58f39bb
@dscape authored
Showing with 38 additions and 44 deletions.
  1. +38 −44 README.md
View
82 README.md
@@ -455,62 +455,56 @@ check out the tests for a fully functioning example.
nano supports making requests using couchdb's [cookie authentication](http://guide.couchdb.org/editions/1/en/security.html#cookies) functionality. there's a [step-by-step guide here](http://mahoney.eu/2012/05/23/couchdb-cookie-authentication-nodejs-nano/), but essentially you just:
-login...
+this is code similar to what a login would look like:
``` 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.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; }
+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) {
+ return callback(err);
+ }
- // send couchdb's cookie right on through to the client
- if (headers && headers['set-cookie']) {
- res.cookie(headers['set-cookie']);
- }
+ if (headers && headers['set-cookie']) {
+ cookies[user] = 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;
-
-if (!auth) { res.send(401); return; }
-// set-up nano with cookie authentication
-nano = require('nano')({ url : 'http://localhost:5984', cookie: 'AuthSession=' + auth });
-
-var alice = nano.use('alice');
-
-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']); }
+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 });
+ ;
- res.send('ok');
- }
- );
-```
+alice.insert(doc, function (err, body, headers) {
+ if (err) {
+ return callback(err);
+ }
-... and finally, logout ...
+ // change the cookie if couchdb tells us too
+ if (headers && headers['set-cookie']) {
+ auth = headers['set-cookie'];
+ }
-``` js
-// the couchdb cookie name is AuthSession
-res.clearCookie('AuthSession');
-res.send('logged out!');
+ callback(null, "It worked");
+});
```
## advanced features
Please sign in to comment.
Something went wrong with that request. Please try again.