From dae6e0761a180093c5292a41e0efc06cc507de94 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Thu, 4 Mar 2021 21:12:49 -0800 Subject: [PATCH] Hashing password and salting. --- package-lock.json | 11 +++++++++++ package.json | 1 + src/accounts/register.js | 17 +++++++++++++++++ src/index.js | 9 +++++++-- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/accounts/register.js diff --git a/package-lock.json b/package-lock.json index 40f8861..83e68ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "bcryptjs": "^2.4.3", "dotenv": "^8.2.0", "fastify": "^3.12.0", "fastify-static": "^4.0.1", @@ -80,6 +81,11 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, + "node_modules/bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms=" + }, "node_modules/bl": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.1.tgz", @@ -938,6 +944,11 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, + "bcryptjs": { + "version": "2.4.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", + "integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms=" + }, "bl": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.1.tgz", diff --git a/package.json b/package.json index cb322b1..d5a5701 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "Bradley Shellnut", "license": "ISC", "dependencies": { + "bcryptjs": "^2.4.3", "dotenv": "^8.2.0", "fastify": "^3.12.0", "fastify-static": "^4.0.1", diff --git a/src/accounts/register.js b/src/accounts/register.js new file mode 100644 index 0000000..6354de6 --- /dev/null +++ b/src/accounts/register.js @@ -0,0 +1,17 @@ +import bcrypt from 'bcryptjs' +const { genSalt, hash } = bcrypt + +export async function registerUser(email, password) { + // generate salt + const salt = await genSalt(10) + console.log('salt', salt) + + // hash with salt + const hashedPassword = await hash(password, salt) + console.log('hashedPassword', hashedPassword) + + // store in database + + // return user from database + +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 32904fd..c072eee 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ import fastifyStatic from 'fastify-static' import path from 'path' import { fileURLToPath } from 'url' import { connectDb } from './db.js' +import { registerUser } from './accounts/register.js' // ESM specific "features" const __filename = fileURLToPath(import.meta.url) @@ -17,8 +18,12 @@ async function startApp() { root: path.join(__dirname, "public"), }) - app.post('/api/register', {}, (request, reply) => { - console.log('request', request.body.email, request.body.password); + app.post('/api/register', {}, async (request, reply) => { + try { + await registerUser(request.body.email, request.body.password) + } catch (e) { + console.error('e', e); + } }) // app.get("/", {}, (request, reply) => {