larvituser
User module for node.js
Basic usage
First fire up the library connections like this:
const UserLib = require('larvituser'),
Intercom = require('larvitamintercom'),
winston = require('winston'),
log = winston.createLogger({'transports': [new winston.transports.Console()]}),
userLib = new UserLib({
'db': require('larvitdb'),
'log': log
});
db.setup(...);
Create a new user in the database, do like this:
const userData = {
'firstname': 'Nisse',
'lastname': 'Nilsson',
'role': [
'user',
'subscriber'
]
}
const user = await userLib.create('myUsername', 'myPassword', userData);
console.log('New user UUID: ' + user.uuid);
When creating a new user you can also give the user a uuid of your choice:
const uuidLib = require('uuid');
const uuid = uuidLib.v1();
const userData = {
'firstname': 'Nisse',
'lastname': 'Nilsson',
'role': [
'user',
'subscriber'
]
}
const user = await userLib.create('myUsername', 'myPassword', userData, uuid);
console.log('New user UUID: ' + user.uuid);
To fetch a user from database based on username and password, do like this:
const user = await userLib.fromUserAndPass('myUsername', 'myPassword');
if ( ! user) {
} else {
console.log('Fetched user ID: ' + user.id);
}
List multiple users
const users = new UserLib.Users({'db': db, 'log': log});
const result = await users.get();
console.log(result.users);
Get distinct values for field from all users
const users = new UserLib.Users({'db': db, 'log': log});
const result = await users.getFieldData('fieldName');
console.log(result);
List multiple users ordered by field
const users = new UserLib.Users({'db': db, 'log': log});
users.order = {
by: 'username',
direction: 'desc'
}
const result = await users.get();
console.log(result.users);
Advanced usage
Add data to a user
await userLib.addUserDataField(userUuid, fieldName, fieldValue);
Check a password for validity
const isValid = await userLib.checkPassword('passwordToTest', 'theHashToTestAgainst');
Create a new user
const user = await userLib.create('username', 'password', {'firstname': 'John', 'lastname': 'Smith'});console.log(user.uuid);
Or set an Uuid manually like this:
const user = await userLib.create('username', 'password', {'firstname': 'John', 'lastname': 'Smith'}, 'f9684592-b245-42fa-88c6-9f16b9236ac3');
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.
const user = await userLib.fromField('firstname', 'John');
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
const user = await userLib.fromFields({'firstname': 'John', 'lastname': 'Smith'});
console.log(user.uuid);
Fetch a user based on just username
const user = await userLib.fromUsername('username');
console.log(user.uuid);
Fetch a user from Uuid
const user = await userLib.fromUuid('f9684592-b245-42fa-88c6-9f16b9236ac3');
console.log(user.uuid);
Get field data from a user
const data = await userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'firstname');
console.log(data);
Replace user fields for a user
IMPORTANT!!! Will clear all data not given in the fields parameter
await userLib.replaceUserFields('f9684592-b245-42fa-88c6-9f16b9236ac3', {'lastname': ['Smith', 'Johnsson']});
const data = await userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'lastname');
console.log(data);
Remove a field from a user
await userLib.rmUserField('f9684592-b245-42fa-88c6-9f16b9236ac3', 'lastname');
const user = await userLib.fromUuid('f9684592-b245-42fa-88c6-9f16b9236ac3');
console.log(user.fields);
Set password for a user
await userLib.setPassword('f9684592-b245-42fa-88c6-9f16b9236ac3', 'newSuperSecretPwd');
To disable a login, use boolean false as the new password:
await userLib.setPassword('f9684592-b245-42fa-88c6-9f16b9236ac3', false);
Set username for a user
await userLib.setUsername('f9684592-b245-42fa-88c6-9f16b9236ac3', 'theNewUsername');
Errors
All functions in the API will throw an exception upon error.
For instance:
const user1 = await userLib.create('nisse', false);
const user2 = await userLib.create('olle', false);
try {
await user2.setUsername('nisse');
} catch (err) {
console.error(err);
}
Tests
Run tests with npm test
, 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
DBCONFFILE=/path/to/config/db_another.json mocha test/test.js