JSPM

bcrypt-promise

2.0.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 1159
    • Score
      100M100P100Q98023F
    • License ISC

    promisify bcrypt

    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-promise

    Example 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);
    }