Question

I am using the most recent versions of Node.js, Express, Nano, and CouchDB, and am trying to get the username and roles of the currently authenticated user. I am using cookie authentication, which is working fine. However, the GET request to _session is returning a blank name and no roles for the user.

Here is the code I am using:

app.get('/_session', function(req, res) {
    var auth = req.cookies['AuthSession'],
        appNano;
    console.log(auth + " from session");
    if (!auth) {
        res.send(401);
    } else {
        appNano = nano({
            url : 'http://localhost:5984', 
            cookie: 'Authsession=' + auth
        });
        appNano.request({
            method: "GET",
            db: "_session"
        }, function(err, body, headers) {
            if (err) {
                res.send(401);
                return;
            }
            if (headers && headers['set-cookie']) {
                res.cookie(headers['set-cookie']);
            }
            console.log(body);
            res.send(body);
        });
    }
});

auth is defined when it is being sent to couch, and it matches the cookie stored on my browser (confirmed with console.log). What do I need to do to get back the username/roles of the authenticated user?

Was it helpful?

Solution

Figured it out. The 'cookie' header should be set to 'AuthSession=' + auth, not 'Authsession=' + auth. Capitalization fail :p.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top