diff --git a/packages/node_modules/pouchdb-auth/lib/sessionapi.js b/packages/node_modules/pouchdb-auth/lib/sessionapi.js index ea7acecb..44a8ca6a 100644 --- a/packages/node_modules/pouchdb-auth/lib/sessionapi.js +++ b/packages/node_modules/pouchdb-auth/lib/sessionapi.js @@ -146,23 +146,31 @@ exports.multiUserLogIn = function (username, password, callback) { function getUserDoc(db, username) { var info = utils.dbDataFor(db); - var userDoc = info.admins[username]; - return Promise.resolve().then(function () { - if (typeof userDoc === "undefined") { - return db.get(docId(username), {conflicts: true}); - } - return userDoc; - }).then(function (doc) { - if ((doc._conflicts || {}).length) { - throw new PouchPluginError({ - status: 401, - name: 'unauthorized', - message: "User document conflicts must be resolved before" + - "the document is used for authentication purposes." - }); - } - return doc - }); + var adminDoc = info.admins[username]; + return db.get(docId(username), {conflicts: true}) + .catch(function (err) { + if (err.name != 'not_found' || typeof adminDoc === 'undefined') { + throw err; + } + }) + .then(function (userDoc) { + if (typeof userDoc !== 'undefined') { + if ((userDoc._conflicts || {}).length) { + throw new PouchPluginError({ + status: 401, + name: 'unauthorized', + message: "User document conflicts must be resolved before" + + "the document is used for authentication purposes." + }); + } + if (typeof adminDoc === 'undefined') { + return userDoc; + } + adminDoc = Object.assign({}, adminDoc); + adminDoc.roles = ['_admin'].concat(userDoc.roles); + } + return adminDoc; + }) } function newSessionId(userDoc, info) {