egg-bcrypt
v1.1.0
Published
egg bcrypt plugin
Downloads
1,727
Readme
egg-bcrypt
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 }; } ...