bcrypt plugin for egg, based on bcrypt (Optimized bcrypt in JavaScript with zero dependencies.)
$ npm i egg-bcrypt --save
- config.default.js
exports.bcrypt = {
saltRounds: 10 // default 10
}
- config/plugin.js
// {app_root}/config/plugin.js
exports.bcrypt = {
enable: true,
package: 'egg-bcrypt'
}
-
[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 }; } ...