Hashing password and salting.

This commit is contained in:
Bradley Shellnut 2021-03-04 21:12:49 -08:00
parent 3b15223b1d
commit dae6e0761a
4 changed files with 36 additions and 2 deletions

11
package-lock.json generated
View file

@ -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",

View file

@ -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",

17
src/accounts/register.js Normal file
View file

@ -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
}

View file

@ -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) => {