larvituser
User module for node.js
Basic usage
First fire up the library connections like this:
const userLib = require('larvituser');
userLib.dataWriter.mode = 'master';
Create a new user in the database, do like this:
const userData = {
'firstname': 'Nisse',
'lastname': 'Nilsson',
'role': [
'user',
'subscriber'
]
}
userLib.create('myUsername', 'myPassword', userData, function(err, user) {
console.log('New user UUID: ' + user.uuid);
});
To fetch a user from database based on username and password, do like this:
userLib.fromUserAndPass('myUsername', 'myPassword', function(err, user) {
if (err) {
throw err;
}
if ( ! user) {
} else {
console.log('Fetched user ID: ' + user.id);
}
});
List multiple users
const users = new userLib.Users();
users.get(function(err, userList) {
if (err) throw err;
console.log(userList);
});
Get distinct values for field from all users
const users = new userLib.Users();
users.getFieldData('fieldName', function(err, result) {
if (err) throw err;
console.log(userList);
});
Advanced usage
Add data to a user
userLib.addUserDataField(userUuid, fieldName, fieldValue, function(err) {
});
Check a password for validity
userLib.checkPassword('passwordToTest', 'theHashToTestAgainst', function(err, result) {
});
Create a new user
userLib.create('username', 'password', {'firstname': 'John', 'lastname': 'Smith'}, function(err, user) {
console.log(user.uuid);
});
Or set an Uuid manually like this:
userLib.create('username', 'password', {'firstname': 'John', 'lastname': 'Smith'}, 'f9684592-b245-42fa-88c6-9f16b9236ac3', function(err, user) {
console.log(user.uuid);
});
Fetch a user based on a field
Will fetch the first occurance in the database with this field name and field value.
userLib.fromField('firstname', 'John', function(err, user) {
console.log(user.uuid);
});
Fetch a user based on several fields
Will fetch the first occurance in the database that matches all these field names and field values
userLib.fromFields({'firstname': 'John', 'lastname': 'Smith'}, function(err, user) {
console.log(user.uuid);
});
Fetch a user based on just username
userLib.fromUsername('username', function(err, user) {
console.log(user.uuid);
});
Fetch a user from Uuid
userLib.fromUuid('f9684592-b245-42fa-88c6-9f16b9236ac3', function(err, user) {
console.log(user.uuid);
});
Get field data from a user
userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'firstname', function(err, data) {
console.log(data);
});
Replace user fields for a user
IMPORTANT!!! Will clear all data not given in the fields parameter
userLib.replaceUserFields('f9684592-b245-42fa-88c6-9f16b9236ac3', {'lastname': ['Smith', 'Johnsson']}, function(err) {
userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'lastname', function(err, data) {
console.log(data);
});
});
Remove a field from a user
userLib.rmUserField('f9684592-b245-42fa-88c6-9f16b9236ac3', 'lastname', function(err) {
userLib.fromUuid('f9684592-b245-42fa-88c6-9f16b9236ac3', function(err, user) {
console.log(user.fields);
});
});
Set password for a user
userLib.setPassword('f9684592-b245-42fa-88c6-9f16b9236ac3', 'newSuperSecretPwd', function(err) {
});
To disable a login, use boolean false as the new password:
userLib.setPassword('f9684592-b245-42fa-88c6-9f16b9236ac3', false, function(err) {
});
Set username for a user
userLib.setUsername('f9684592-b245-42fa-88c6-9f16b9236ac3', 'theNewUsername', function(err) {
});
Tests
Run tests with mocha, make sure to have an empty database configured for tests to pass correctly!
The default config file will be application path/config/db_test.json
Or a custom one can be used by running
mocha test/test.js /path/to/config/db_another.json