From 6c43ef6e1af970ec4bc6955b81e84f5baaab117b Mon Sep 17 00:00:00 2001 From: Schnell Henrik Date: Fri, 23 Oct 2015 22:57:02 +0200 Subject: [PATCH] Fixed: Could not login with existing user. PUT /-/user/:org_couchdb_user should login with existing user when password matches the username, added auth.authenticate call before auth.add_user. --- lib/index-api.js | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/index-api.js b/lib/index-api.js index f91b6245..f7a2f381 100644 --- a/lib/index-api.js +++ b/lib/index-api.js @@ -180,23 +180,36 @@ module.exports = function(config, auth, storage) { return next( Error[422]('user/password is not found in request (npm issue?)') ) } } - auth.add_user(req.body.name, req.body.password, function(err, user) { - if (err) { - if (err.status >= 400 && err.status < 500) { - // With npm registering is the same as logging in, - // and npm accepts only an 409 error. - // So, changing status code here. - return next( Error[409](err.message) ) - } - return next(err) + // try to login first + auth.authenticate(req.body.name, req.body.password, function (err, user) { + if (!err) { + // user already exists, login + req.remote_user = user + res.status(201) + return next({ + ok: "you are authenticated as '" + req.body.name + "'", + token: token, + }) } + // could not login, try to create user + auth.add_user(req.body.name, req.body.password, function(err, user) { + if (err) { + if (err.status >= 400 && err.status < 500) { + // With npm registering is the same as logging in, + // and npm accepts only an 409 error. + // So, changing status code here. + return next( Error[409](err.message) ) + } + return next(err) + } - req.remote_user = user - res.status(201) - return next({ - ok: "user '" + req.body.name + "' created", - //token: auth.issue_token(req.remote_user), - token: token, + req.remote_user = user + res.status(201) + return next({ + ok: "user '" + req.body.name + "' created", + //token: auth.issue_token(req.remote_user), + token: token, + }) }) }) }