简体   繁体   English

使用ExpressJS和CouchDB进行用户身份验证

[英]User Authentication with ExpressJS and CouchDB

I'm trying to login a CouchDB User into my express app via a frontend form and store the login in the session. 我正在尝试通过前端表单将CouchDB用户登录到我的express应用中,并将登录信息存储在会话中。 What have so far is the following: 到目前为止,以下内容是:

app.js: app.js:

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: 并在我的login.js中:

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;

I don't know how to go on in my login.js route. 我不知道如何在我的login.js路线中进行。 Any help is appreciated. 任何帮助表示赞赏。

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: 更新-由于我无法完全理解代码而无法在下面工作,因此研究使我找到了以下解决方案:

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 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?!? 当我通过HttpRequester进行相同的请求时,我得到状态码200和{“ ok”:true,“ name”:null,“ roles”:[“ _ admin”]} ..但通过nodejs,即使这样做,它也不会这样做应该一样吗?!?

To validate user credentials against CouchDB just follow the example from CouchDB documentation . 要根据CouchDB验证用户凭据,只需遵循CouchDB文档中的示例。

curl -X POST http://localhost:5984/_session -d 'name=jan&password=apple'

After successful authentication you can keep CouchDB credentials the session storage . 成功通过身份验证后,您可以保留CouchDB凭据作为会话存储

I created a "proof of concept" code which is probably even not correct since i am not nodejs expert . 我创建了一个“概念验证”代码,由于我不是nodejs专家 ,因此可能甚至不正确。 But after some tuning it should work. 但是经过一些调整后,它应该可以工作了。

var http = require('http');

router.post('/login', function(req, res) {
    var session = req.session;
    request.post('http://localhost:5984/_session')
        .auth(req.data.username, req.data.password, true)
        .on('response', function(response) {
            if(response.statusCode == 200) {
                session.couchSession = req.data.username + ':' + req.data.password;
                res.status(200);
                res.send();
            } else {
                res.status(400);
                res.send('Wrong credentials');
            }
        });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM