larvituser
Advanced tools
Comparing version 0.4.8 to 0.4.9
@@ -350,3 +350,3 @@ 'use strict'; | ||
* @param str fieldValue | ||
* @param func cb(err, user) - "user" being a new user object or boolean false on failed login | ||
* @param func cb(err, user) - "user" being a new user object or boolean false on failed search | ||
*/ | ||
@@ -382,2 +382,42 @@ function fromField(fieldName, fieldValue, cb) { | ||
/** | ||
* Create a user object from fields | ||
* IMPORTANT! Only fetches first matching user that matches all fields! | ||
* | ||
* @param obj fields - {'fieldName': 'fieldValue', 'fieldName2': 'fieldValue2'} | ||
* @param func cb(err, user) - "user" being a new user object or boolean false on failed search | ||
*/ | ||
function fromFields(fields, cb) { | ||
checkDbStructure(function() { | ||
var dbFields = [], | ||
fieldName, | ||
sql; | ||
sql = 'SELECT uuid FROM user_users u\n'; | ||
sql += 'WHERE 1 + 1\n'; | ||
for (fieldName in fields) { | ||
sql += ' AND uuid IN (SELECT userUuid FROM user_users_data WHERE data = ? fieldId = (SELECT id FROM user_data_fields WHERE name = ?))\n'; | ||
dbFields.push(_.trim(fields[fieldName])); | ||
dbFields.push(_.trim(fieldName)); | ||
} | ||
sql += 'LIMIT 1'; | ||
db.query(sql, dbFields, function(err, rows) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (rows.length === 0) { | ||
cb(null, false); | ||
return; | ||
} | ||
fromUuid(utils.formatUuid(rows[0].userUuid), cb); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Create a user object from username and password | ||
@@ -1044,2 +1084,3 @@ * | ||
exports.fromField = fromField; | ||
exports.fromFields = fromFields; | ||
exports.fromUserAndPass = fromUserAndPass; | ||
@@ -1046,0 +1087,0 @@ exports.fromUsername = fromUsername; |
@@ -42,5 +42,5 @@ { | ||
}, | ||
"version": "0.4.8", | ||
"version": "0.4.9", | ||
"readmeFilename": "README.md", | ||
"license": "MIT" | ||
} |
@@ -92,2 +92,12 @@ [![Build Status](https://travis-ci.org/larvit/larvituser.svg?branch=master)](https://travis-ci.org/larvit/larvituser) | ||
#### Fetch a user based on several fields | ||
Will fetch the first occurance in the database that matches all these field names and field values | ||
```javascript | ||
userLib.fromFields({'firstname': 'John', 'lastname': 'Smith'}, function(err, user) { | ||
console.log(user.uuid); // f9684592-b245-42fa-88c6-9f16b9236ac3 | ||
}); | ||
``` | ||
#### Fetch a user based on just username | ||
@@ -94,0 +104,0 @@ |
@@ -154,3 +154,2 @@ 'use strict'; | ||
describe('logins', function() { | ||
it('should log the created user in by username', function(done) { | ||
@@ -157,0 +156,0 @@ userLib.fromUsername('lilleman', function(err, user) { |
43740
1208
186