code refactor and implement password hashing
This commit is contained in:
@@ -39,7 +39,7 @@ async function insertUser(username, password){
|
||||
|
||||
try {
|
||||
const signupData = await client.query(query, values);
|
||||
console.log('Account created:', signupData.rows[0]);
|
||||
console.log('Account created:', signupData.rows[0].username);
|
||||
} catch (err) {
|
||||
console.error('Error inserting data:', err.stack);
|
||||
}
|
||||
36
index.js
36
index.js
@@ -1,13 +1,13 @@
|
||||
const express = require('express');
|
||||
const session = require('express-session');
|
||||
const path = require('path');
|
||||
const { client } = require('./app/db');
|
||||
const { client } = require('./backend/db');
|
||||
const port = 3000
|
||||
const app = express()
|
||||
const { insertUser, isUserExists, loginUser } = require('./app/db');
|
||||
const crypto = require('crypto');
|
||||
const algorithm = 'aes-256-cbc';
|
||||
const key = process.env.ENCRYPT_KEY;
|
||||
const { insertUser, isUserExists, loginUser } = require('./backend/db');
|
||||
const bcrypt = require('bcrypt');
|
||||
const {hash} = require("bcrypt");
|
||||
const saltRounds = 10;
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
@@ -46,9 +46,22 @@ app.post('/auth/signup', async (req, res) => {
|
||||
res.status(500).send('User already exists!');
|
||||
return;
|
||||
}
|
||||
// put user data to database
|
||||
await insertUser(username, password);
|
||||
res.status(200).send("Account successfully created <a href=/login>Login screen</a>");
|
||||
|
||||
// hash password
|
||||
bcrypt.genSalt(saltRounds, function(err, salt) {
|
||||
if(err) {
|
||||
console.log('Error generating salt:', err);
|
||||
res.status(500).send('Error generating salt');
|
||||
}
|
||||
bcrypt.hash(password, salt, function(err, hash) {
|
||||
if(err) {
|
||||
console.log('Error hashing password:', err);
|
||||
res.status(500).send('Error hashing password');
|
||||
}
|
||||
insertUser(username, hash);
|
||||
res.status(200).send("Account successfully created <a href=/login>Login screen</a>");
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error inserting data:', err);
|
||||
res.status(500).send('Error inserting data');
|
||||
@@ -57,18 +70,19 @@ app.post('/auth/signup', async (req, res) => {
|
||||
|
||||
// serving the login page
|
||||
app.get('/login', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/public/routes/login.html'));
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/login.html'));
|
||||
})
|
||||
|
||||
// serving the signup page
|
||||
app.get('/signup', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/public/routes/signup.html'));
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/signup.html'));
|
||||
})
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/public/routes/index.html'));
|
||||
res.sendFile(path.join(__dirname, '/frontend/routes/index.html'));
|
||||
})
|
||||
// run server
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
})
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
},
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"crypto": "^1.0.1",
|
||||
"bcrypt": "^5.1.1",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
"express-session": "^1.18.0",
|
||||
|
||||
Reference in New Issue
Block a user