Skip to content

Instantly share code, notes, and snippets.

@getaclue00
Last active March 21, 2020 14:57
Show Gist options
  • Save getaclue00/62142f5b829c4594a2f13bff7505f90d to your computer and use it in GitHub Desktop.
Save getaclue00/62142f5b829c4594a2f13bff7505f90d to your computer and use it in GitHub Desktop.
code to follow along my write up of a bcrypt expressJS example
// index.js
const express = require('express');
// import bcrypt and set the salt rounds
// read about salt in the readme
const bcrypt = require('bcrypt');
const bcryptSALTrounds = 10;
const app = express();
const PORT = 3000;
// REQUIREMENT FUNCTION #1
// http://localhost:3000/hash/dasdas
app.get('/hash/:stuff', async (req, res, next) => {
// want to inspect params? uncomment :
// console.log(req.params);
const { stuff } = req.params;
const hash = await bcrypt.hash(`${stuff}`, bcryptSALTrounds);
res.send({ message: `${hash}` });
});
// REQUIREMENT FUNCTION #2
// http://localhost:3000/compare/$2b$10$DB0melt1B.3Fn7MfXwC17OIevWVhtSgXMrINkg9M4SEHdFgM6Myqm/dasdas
app.get('/compare/:hash/:stuff', async (req, res, next) => {
// want to inspect params? uncomment :
// console.log(req.params);
const { hash, stuff } = req.params;
const match = await bcrypt.compare(stuff, hash);
if (match) {
res.send({ message: 'match' });
} else {
res.send({ message: `not match` });
}
});
app.listen(PORT, () => {
console.log(`listening on port : ${PORT}`);
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment