Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

larvituser

Package Overview
Dependencies
Maintainers
1
Versions
261
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

larvituser

User module for node.js

  • 0.4.14
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
332
increased by25.28%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status Dependencies

larvituser

User module for node.js

Basic usage

First fire up the library connections like this:

var userLib = require('larvituser');

Create a new user in the database, do like this:

var 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) {
		// No match found, or other more serious error
	} else {
		console.log('Fetched user ID: ' + user.id);
	}
});

Advanced usage

Add data to a user
userLib.addUserField(userUuid, fieldName, fieldValue, function(err) {
	// Field have been added
});
Check a password for validity
userLib.checkPassword('passwordToTest', 'theHashToTestAgainst', function(err, result) {
	// Result being either true or false
});
Create a new user
userLib.create('username', 'password', {'firstname': 'John', 'lastname': 'Smith'}, function(err, user) {
	console.log(user.uuid); // 91f15599-c1fa-4051-9e0e-906cab9819fe (or rather, a random 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); // 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.

userLib.fromField('firstname', 'John', function(err, user) {
	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

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
userLib.fromUsername('username', function(err, user) {
	console.log(user.uuid); // f9684592-b245-42fa-88c6-9f16b9236ac3
});
Fetch a user from Uuid
userLib.fromUuid('f9684592-b245-42fa-88c6-9f16b9236ac3', function(err, user) {
	console.log(user.uuid); // f9684592-b245-42fa-88c6-9f16b9236ac3
});
Get field data from a user
userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'firstname', function(err, data) {
	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

userLib.replaceUserFields('f9684592-b245-42fa-88c6-9f16b9236ac3', {'lastname': ['Smith', 'Johnsson']}, function(err) {
	// The field "lastname" will now be replaced with the two values "Smith" and "Johnsson"
	// And all other fields will be removed

	userLib.getFieldData('f9684592-b245-42fa-88c6-9f16b9236ac3', 'lastname', function(err, data) {
		console.log(data); // ['Smith', 'Johnsson']
	});
});
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); // {'firstname': ['John']}
	});
});
Set password for a user
userLib.setPassword('f9684592-b245-42fa-88c6-9f16b9236ac3', 'newSuperSecretPwd', function(err) {
	// Now the users password is updated to "newSuperSecretPwd"
});

To disable a login, use boolean false as the new password:

userLib.setPassword('f9684592-b245-42fa-88c6-9f16b9236ac3', false, function(err) {
	// This user can no longer login
});
Set username for a user
userLib.setUsername('f9684592-b245-42fa-88c6-9f16b9236ac3', 'theNewUsername', function(err) {
	// Now the users password is updated to "theNewUsername"
});

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

Keywords

FAQs

Package last updated on 25 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc