I am currently working on implementing a CouchDB User login into my express app using a frontend form and storing the login information in the session. Here is what I have done so far:
In my app.js file:
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);
And in my login.js file:
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) {
// This section needs to be filled based on requirements
//res.send(req.body.username)
});
module.exports = router;
I am seeking guidance on how to proceed with the route in my login.js file. Any assistance would be greatly appreciated.
An update - I encountered issues with the previous code due to lack of clarity, after further research I found a solution that worked for me:
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 && response.statusCode == 200) {
console.log('authenticated');
}else{
console.log('not authenticated');
res.redirect('/')
}
});
});
When testing the same request using HttpRequester, I received a Statuscode 200 along with {"ok":true,"name":null,"roles":["_admin"]}. However, when attempting it through Node.js, it did not work as expected even though the requests were similar. Any insights on this discrepancy?