npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

larvituser

v0.24.1

Published

User module for node.js

Downloads

2,391

Readme

Build Status

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'),

		// Optional parameters
		'log': log
	});

db.setup(...); // See https://github.com/larvit/larvitdb for configuration details

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) {
	// No match found, or other more serious error
} 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); // An array of objects

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); // An array of strings

List multiple users ordered by field

const users = new UserLib.Users({'db': db, 'log': log});

users.order = {
	by: 'username', // Sorting by something else than uuid or username the field needs to be included in "returnFields"
	direction: 'desc' // "asc" is default
}

const result = await users.get();
console.log(result.users); // An array of objects

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); // 91f15599-c1fa-4051-9e0e-906cab9819fe (or rather, a random 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); // f9684592-b245-42fa-88c6-9f16b9236ac3

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); // f9684592-b245-42fa-88c6-9f16b9236ac3

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); // f9684592-b245-42fa-88c6-9f16b9236ac3

Fetch a user based on just username

const user = await userLib.fromUsername('username');
console.log(user.uuid); // f9684592-b245-42fa-88c6-9f16b9236ac3 or user will be false if no user is found

Fetch a user from Uuid

const user = await userLib.fromUuid('f9684592-b245-42fa-88c6-9f16b9236ac3');
console.log(user.uuid); // f9684592-b245-42fa-88c6-9f16b9236ac3

Get field data from a user

const data = await userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'firstname');
console.log(data); // ['John'] - Observe this will always be an array with values, since a field can hold several values

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']});
// The field "lastname" will now be replaced with the two values "Smith" and "Johnsson"
// And all other fields will be removed

const data = await userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'lastname');
console.log(data); // ['Smith', 'Johnsson']

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); // {'firstname': ['John']}

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'); // Will throw since username "nisse" is already taken
} 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