1.0.1 • Published 6 years ago

node-password-util v1.0.1

Weekly downloads
8
License
MIT
Repository
github
Last release
6 years ago

Node Password Util

Build Status Code Coverage

Hash, compare, and reset passwords for your nodejs app

Requirements: Node.js 0.10+

Installation

yarn add node-password-util

Usage

To replace a cleartext password with a one-way hash:

import {hash} from 'node-password-util';

const user = {
  name: 'Joe Blog',
  password: 'somelonghardtoguesspassword'
};

hash(user).then(() => {
  console.log(user);
  // user = {
  //   name: 'Joe Blog',
  //   password: '$2a$10$Lk1PZU6zlofxzbvCpbBbNunh0lDVHkp.vnB5C0RDPmndWGrTnH8Fq'
  // }
});

To verify a password against a user's hash:

import {compare} from 'node-password-util';

const user = {
  name: 'Joe Blog',
  password: '$2a$10$Lk1PZU6zlofxzbvCpbBbNunh0lDVHkp.vnB5C0RDPmndWGrTnH8Fq'
};

compare(user, 'somelonghardtoguesspassword').then(isMatch => {
  console.log(isMatch);
  // true
});

To generate a reset password token with expiry:

import {reset} from 'node-password-util';

const user = {
  name: 'Joe Blog',
  password: '$2a$10$Lk1PZU6zlofxzbvCpbBbNunh0lDVHkp.vnB5C0RDPmndWGrTnH8Fq'
};

reset(user).then(() => {
  console.log(user);
  // user = {
  //   name: 'Joe Blog',
  //   password: '$2a$10$Lk1PZU6zlofxzbvCpbBbNunh0lDVHkp.vnB5C0RDPmndWGrTnH8Fq',
  //   resetPasswordToken: '80a1b278254d476895d340bf54bfd5a0da2635a7',
  //   resetPasswordExpires: 1476881237903 (expires in an hour)
  // }
});

To update a user's password:

import {update} from 'node-password-util';

const user = {
  name: 'Joe Blog',
  password: '$2a$10$Lk1PZU6zlofxzbvCpbBbNunh0lDVHkp.vnB5C0RDPmndWGrTnH8Fq',
  resetPasswordToken: '80a1b278254d476895d340bf54bfd5a0da2635a7',
  resetPasswordExpires: 1476881237903
};

update(user, 'someothernewreallylongpassword').then(() => {
  console.log(user);
  // user = {
  //   name: 'Joe Blog',
  //   password: '$2a$10$eiEx4/zisb59vSq9sZbbPeR5cYzYgPcEbDDO6719LPEjCwkvN6g0C'
  // }
});

Licence

MIT

1.0.1

6 years ago

1.0.0

6 years ago

0.1.3

6 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago