my-user v.0.2.0
my-user
is my simple user objects for me, supporting authentication.
User objects hold user id, authentication data, and other data.
Installation
npm install my-user
TypeScript support
Available. Requites TypeScript >= 2.0
Usage
Init UserConfig
and create User
:
var userconfig=; // Create uservar user=userconfig; // Set password and extra datauser; //authenticateconsole; //falseconsole; //true //read datavar data=user;console;
Versioning and auth methods
User objects have versions.
Different versions may have different auth methods.
var userconfig=; //Set salt length by bytesuserconfig; //version 1 -> 16 bytes //User-defined salt generatoruserconfig; //Password hash functionuserconfig; //User-defined hash functionuserconfig; var user=userconfig; //indicate user versionuser; console; //true //if version is omitted, it will be the latestuser; console; //true
APIs
init([options])
Returns new instance of UserConfig
.
- options.freeze (boolean): Data returned by
user.getData()
is frozen byObject.freeze
. (default: false) - options.deepcopy (boolean): Data passed to
user.setData()
is deeply cloned.
userconfig.create([userid])
Returns new instance of User
.
userconfig.setSalt(version,bytes)
Set salt-generation function of version version
to crypto.pseudoRandomBytes(bytes)
.
Salts will be represented by hex string.
userconfig.setSalt(version,func)
Set salt-generation function of version version
to func
.
func
takes no argument and returns string.
userconfig.setPasswordHash(version,type)
Set password hash function of version version
to type
.
type
is string representing hash function supported by crypto
.
userconfig.setPasswordHash(version,func)
Set password hash function of version version
to func
.
func
takes 2 arguments salt
and password
and returns string.
userconfig.getSalt([version])
returns salt-generation function of version version
(the latest if omitted).
userconfig.getPasswordHash([version])
returns password hash function of version version
(the latest if omitted).
user.id
User id string.
user.version
The version of user represented by integer.
user.salt
Salt string.
user.password
Hashed password string.
user.setData(data[, password][, version])
Set user's extra data, optionally password, and optionally version.
If version
is omitted, it will be the latest.
password
is required when password hash function changes by upgrading/downgrading.
user.writeData(data)
Partially overrides user's data by data
.
This method does not change the version of user
.
user.getData()
Returns user's current extra data.
user.auth(password)
Checks if password
is currect. Returns boolean value.