https://github.com/sepmein/bcrypt-promise
promisify bcrypt library
https://github.com/sepmein/bcrypt-promise
Last synced: 3 months ago
JSON representation
promisify bcrypt library
- Host: GitHub
- URL: https://github.com/sepmein/bcrypt-promise
- Owner: sepmein
- License: apache-2.0
- Created: 2016-08-27T11:42:08.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-19T05:14:58.000Z (over 8 years ago)
- Last Synced: 2025-01-22T20:30:20.985Z (5 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bcrypt-promise
Promisify [node.bcrypt.js](https://github.com/ncb000gt/node.bcrypt.js) library
## Usage
```bash
npm install bcrypt-promise
```## Example with `.next()`
```javascript
let bcrypt = require('bcrypt-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.
```javascript
// 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
```javascript
try {
let same = yield bcrypt.compare(password, hash);
} catch(error) {
console.log(error);
}
```