Users
Installation
npm i --save useraccounts
Usage
Server
SQL-Script to create the necessary Table
CREATE TABLE "Users" (
"ID" SERIAL,
"Name" character varying (32) NOT NULL,
"EMail" character varying (320) NOT NULL,
"Password" character (128) NOT NULL,
"Salt" character (16) NOT NULL
);
- all passwords are expected to be a 128 character hexadecimal value (sha 512)
- all salts are expected to be a 16 character value
const postgres = require('pg'... // any postgres module that takes the same arguments and returns the same structure as the packages 'pg' or 'postgresupdater'
const pg = new postgres(.. // initialize the module and open a database connection
const users = require('useraccounts').UserServer(pg);
// to enable users to log in, they'll need the salt for their account
await users.getSalt(user) // takes email or username as argument and returns the salt as hex string
// authentication
// returns the user id
// returns -1 if failed
await users.authenticate(
user, // takes email or username
password // the hash that was sent by the client
);
// add new users
// returns the user id
// returns -1 if username or email already exist
await users.add(name, email, password, salt);
// set a users password
await users.setPassword(id, password, salt);
// set a users name
await users.setName(id, name);
// set a users email
await users.setEmail(id, email);
// get name and email
await users.getData(id); // returns {name:'', email:''}
// get the id for an email or name
await users.search(user); // returns -1 if no user was found
// delete a user
await users.delete(id);
Client
const Client = require('useraccounts').Client;
// create a hash and salt for a plain text password
const hash = Client.hash('abc123');
console.log(hash.digest, hash.salt);
// hash with a given salt
const hash = Client.hash('abc123', '0123456789abcdef');
console.log(hash.digest, hash.salt); // the salt will be unchanged