Package Exports
- bcrypt-promise
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bcrypt-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
bcrypt-promise
Promisify node.bcrypt.js library
Usage
npm install bcrypt-promiseExample with .next()
let bcrypt = require('bcypt-promise');
bcrypt.compare(password, hash)
.then(function(err, same){
if(same) {
// do something
} else {
// do otherthing
}
});Example with koa
Just use keyword yield before bcrypt-promise function then get the result.
// require library and initiate koa server
let bcrypt = require('bcrypt-promise');
let koa = require('koa');
let app = koa();
app.listen(3000);
// USE LIBRARY
app.use(function*(next){
// get password from somewhere
let password = this.request.body.password;
// get hash from somewhere
let hash = yield db.findHash(_id);
/*
* use this library
*/
let same = yield bcrypt.compare(password, hash);
if(same) {
this.body = 'Yeah!';
} else {
this.body = 'Whops!';
}
yield next;
});Use try-catch to handle with errors
try {
let same = yield bcrypt.compare(password, hash);
} catch(error) {
console.log(error);
}