Skip to content

yolopunk/egg-bcrypt

Repository files navigation

egg-bcrypt

NPM version build status Test coverage David deps Known Vulnerabilities npm download

bcrypt plugin for egg, based on bcrypt (Optimized bcrypt in JavaScript with zero dependencies.)

Install

$ npm i egg-bcrypt --save

Configuration

  • config.default.js
exports.bcrypt = {
  saltRounds: 10 // default 10
}
  • config/plugin.js
// {app_root}/config/plugin.js
exports.bcrypt = {
  enable: true,
  package: 'egg-bcrypt'
}

Usage

  • [async] To hash a password

    @params plainText(string)

    @return Promise

    ctx.genHash(plainText)
  • [async] To check a password

    @params plainText (string)

    @params hash (string)

    @return Boolean true/false

    ctx.compare(plainText, hash)
  • example

      // {app_root}/app/contoller/user.js
      ...
      async generate() {
        const hash = await this.ctx.genHash(this.ctx.request.body.plainText);
        // Store hash in your password DB
        ...
      }
    
      async compare() {
        const { hash, plainText } = this.ctx.request.body;
        const checked = await this.ctx.compare(plainText, hash);
        this.ctx.body = { checked };
      } 
      ...

License

MIT