title: User Authentication with ExpressJS and CouchDB
body: <p>I'm trying to login a CouchDB User into my express app via a frontend form and store the login in the session. What have so far is the following:</p>
  
  <p>app.js:</p>
  
  <pre><code>var express = require('express');
  var couchUser = require('express-user-couchdb');
  var session = require('express-session');
  
  var login = require('./routes/login');
  
  var app = express();
  
  app.use(couchUser({
      users: 'http://localhost:5984/_users',
      request_defaults: {
          auth: {
              user: 'admin',
              pass: 'adminpw'
          }
      }
  }));
  
  app.use(session({ secret: 'secretstring'}));
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', login);
  </code></pre>
  
  <p>and in my login.js:</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var couchUser = require('express-user-couchdb');
  
  /* GET users listing. */
  router.get('/', function(req, res, next) {
      res.render('login', {title: 'Login'});
  });
  
  router.post('/login', function(req, res) {
      // Don't know what to put here
      //res.send(req.body.username)
  });
  
  module.exports = router;
  </code></pre>
  
  <p>I don't know how to go on in my login.js route. Any help is appreciated.</p>
  
  <p>Update - Since I couldn't get the code underneath to work because I didn't understand it completely, research lead me to the following solution:</p>
  
  <pre><code>router.post('/', function(req, res) {
  
      var options = {
          url: 'http://localhost:5984/_session',
          method: 'POST',
          json: {
              "name": "admin",
              "password": "password"
          }
      };
  
  
      request(options, function (error, response, body) {
          if (!error &amp;&amp; response.statusCode == 200) {
              console.log('authenticated');
          }else{
              console.log('not authenticated');
              res.redirect('/')
          }
      });
  });
  </code></pre>
  
  <p>When I do the same request via HttpRequester I get Statuscode 200 and {"ok":true,"name":null,"roles":["_admin"]} .. but via nodejs it won't do it even though it should be the same?!?</p>