Skip to content
Browse files

Add anonymous support

  • Loading branch information...
1 parent b82c8a6 commit 6d0949af339520d7330074e10c6071d0f3067e5e @isaacs isaacs committed
Showing with 64 additions and 18 deletions.
  1. +14 −1 README.md
  2. +26 −17 couch-login.js
  3. +24 −0 test/basic.js
View
15 README.md
@@ -90,7 +90,7 @@ http.createServer(function (req, res) {
```
## Class: CouchLogin
-### new CouchLogin(couchdbUrl)
+### new CouchLogin(couchdbUrl, token)
Create a new CouchLogin object bound to the couchdb url.
@@ -111,6 +111,10 @@ identical to the arguments passed to a `request` callback.
* `resp` {HTTP Response} The response from the request to couchdb
* `data` {Object} The parsed JSON data from couch
+If the token is the string "anonymous", then it will not attempt to log
+in before making requests. If the token is not "anonymous", then it
+must be an object with the appropriate fields.
+
### couch.token
* {Object}
@@ -144,6 +148,15 @@ discarded.
Related to tokenGet and tokenSet. Takes a callback which should be
called when the token is deleted.
+### couch.anonymous()
+
+Return a new CouchLogin object that points at the same couchdb server,
+but doesn't try to log in before making requests.
+
+This is handy for situations where the user is not logged in at the
+moment, but a request needs to be made anyway, and does not require
+authorization.
+
### couch.login(auth, callback)
* `auth` {Object} The login details
View
43 couch-login.js
@@ -4,7 +4,7 @@ var request = require('request')
module.exports = CouchLogin
-function CouchLogin (couch) {
+function CouchLogin (couch, tok) {
if (!(this instanceof CouchLogin)) {
return new CouchLogin(couch)
}
@@ -16,9 +16,29 @@ function CouchLogin (couch) {
couch = url.parse(couch)
delete couch.auth
+ if (tok === 'anonymous') tok = NaN
+ this.token = tok
this.couch = url.format(couch)
}
+CouchLogin.prototype =
+{ get: makeReq('GET')
+, del: makeReq('DELETE')
+, put: makeReq('PUT', true)
+, post: makeReq('POST', true)
+, login: login
+, logout: logout
+, decorate: decorate
+, changePass: changePass
+, signup: signup
+, deleteAccount: deleteAccount
+, anon: anon
+, anonymous: anon
+}
+
+Object.defineProperty(CouchLogin.prototype, 'constructor',
+ { value: CouchLogin, enumerable: false })
+
function decorate (req, res) {
req.couch = res.couch = this
@@ -42,23 +62,12 @@ function decorate (req, res) {
return this
}
-CouchLogin.prototype =
-{ get: makeReq('get')
-, del: makeReq('del')
-, put: makeReq('put', true)
-, post: makeReq('post', true)
-, login: login
-, logout: logout
-, decorate: decorate
-, changePass: changePass
-, signup: signup
-, deleteAccount: deleteAccount
+function anon () {
+ return new CouchLogin(this.couch, NaN)
}
-Object.defineProperty(CouchLogin.prototype, 'constructor',
- { value: CouchLogin, enumerable: false })
-
function makeReq (meth, body, f) { return function madeReq (p, d, cb) {
+ f = f || (this.token !== this.token)
if (!f && !valid(this.token)) {
// lazily get the token.
if (this.tokenGet) return this.tokenGet(function (er, tok) {
@@ -80,13 +89,13 @@ function makeReq (meth, body, f) { return function madeReq (p, d, cb) {
var h = {}
, u = url.resolve(this.couch, p)
- , req = { uri: u, headers: h, json: true, body: d }
+ , req = { uri: u, headers: h, json: true, body: d, method: meth }
if (this.token) {
h.cookie = 'AuthSession=' + this.token.AuthSession
}
- request[meth](req, function (er, res, data) {
+ request(req, function (er, res, data) {
// update cookie.
if (er || res.statusCode !== 200) return cb(er, res, data)
addToken.call(this, res)
View
24 test/basic.js
@@ -223,6 +223,30 @@ tap.test('logged out post', function (t) {
})
})
+tap.test('anonymous put', function (t) {
+ var guy = couch.anonymous()
+ guy.put('/remember', {november:5}, function (er, res, data) {
+ t.ifError(er)
+ if (er) return t.end()
+ t.equal(res.statusCode, 401)
+ t.deepEqual(data, { error: 'unauthorized'
+ , reason: 'You are not a server admin.' })
+ t.end()
+ })
+})
+
+tap.test('anonymous get', function (t) {
+ var guy = couch.anonymous()
+ guy.get('/', function (er, res, data) {
+ t.ifError(er)
+ if (er) return t.end()
+ t.equal(res.statusCode, 200)
+ t.deepEqual(data, { couchdb: 'Welcome', version: '1.2.0' })
+ t.end()
+ })
+})
+
+
var signupUser = { name: 'test-user-signup', password: 'signup-test' }

0 comments on commit 6d0949a

Please sign in to comment.
Something went wrong with that request. Please try again.