Search code examples
javascriptbrowserpasswordsbcryptpassword-hash

How to use bcrypt hash on the browser side?


I'm already using bcrypt.hashSync() on the NodeJS server side (included with NPM), but I can't use it on the browser side: the 'bcrypt' object is undefined when I call it with:

    <script src="node_modules/bcryptjs/dist/bcrypt.min.js"></script>

    ...
    EnteredPassword = bcrypt.hashSync(pwd, 10);

Solution

  • After some research, the solution is very simple: you have to add a prefix "dcodeIO." before "bcrypt" :

        <script src="node_modules/bcryptjs/dist/bcrypt.min.js"></script>
    
        ...
        EnteredPassword = dcodeIO.bcrypt.hashSync(pwd, 10);
    
     (or, as async...)
    
        EnteredPassword = await dcodeIO.bcrypt.hash(pwd, 10);