You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2022/04/12 07:53:10 UTC

[GitHub] [couchdb-nano] Morched23MJ commented on issue #145: Cookie auth don't work?

Morched23MJ commented on issue #145:
URL: https://github.com/apache/couchdb-nano/issues/145#issuecomment-1096292659

   _For future visitors_
   
   For some reason, the documentation is not really clear on this auth thingy.
   
   There are basically two ways to authenticate per my little experience using _nano_:
   1. Use `nano.auth` method:
   ```
   const nano = require('nano');
   const COUCHDB_URL = process.env.COUCHDB_URL || 'http://localhost:5984';
   var couchServer = nano({ url: COUCHDB_URL });
   
   couchServer.auth(
       'admin',
       'admin',
       function (err, response, headers) {
           console.log(err, response, headers)
   
           // set cookie to the returned headers cookie
           couchServer = nano({ url: COUCHDB_URL, cookie: headers['set-cookie'] })
   
           // do authenticated work here
           couchServer.db.create('test1', function (err) {
               if (err) {
                   console.error(err);
               }
           });
       })
   
   ```
   2. Use `requestDefaults` property when initializing _nano_ object to set authentication credentials:
   ```
   const nano = require('nano');
   const COUCHDB_URL = process.env.COUCHDB_URL || 'http://localhost:5984';
   var couchServer = nano({ url: COUCHDB_URL, requestDefaults: { auth: { username: 'admin', password: 'admin' } } });
   
   // do authenticated work here
   couchServer.db.list().then(console.log)
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org