I would not use / fork Connect-Auth. This connection plugin breaks the idea / architecture of the onion ring and makes (IMHO) your code unreadable / brings unnecessary complexity.
Authentication is too easy for the library. (If you are talking about simple user login)
I use self-signed authorization. Below you can find a simplified version. It also depends on the session files, but it can easily be replaced with persistent cookies.
Very simple authentication with connection
(It is completed. Just do it for testing)
var connect = require('connect');
var urlpaser = require('url');
var authCheck = function (req, res, next) {
url = req.urlp = urlpaser.parse(req.url, true);
if ( url.pathname == "/logout" ) {
req.session.destroy();
}
if (req.session && req.session.auth == true) {
next();
return;
}
if ( url.pathname == "/login" &&
url.query.name == "max" &&
url.query.pwd == "herewego" ) {
req.session.auth = true;
next();
return;
}
res.writeHead(403);
res.end('Sorry you are unauthorized.\n\nFor a login use: /login?name=max&pwd=herewego');
return;
}
var helloWorldContent = function (req, res, next) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('authorized. Walk around :) or use /logout to leave\n\nYou are currently at '+req.urlp.pathname);
}
var server = connect.createServer(
connect.logger({ format: ':method :url' }),
connect.cookieParser(),
connect.session({ secret: 'foobar' }),
connect.bodyParser(),
authCheck,
helloWorldContent
);
server.listen(3000);
source
share