crypto library for encrypting passwords
This commit is contained in:
@@ -27,6 +27,7 @@ async function isUserExists(username) {
|
||||
}
|
||||
}
|
||||
|
||||
// function for signup
|
||||
// function for putting user data to database
|
||||
async function insertUser(username, password){
|
||||
const query = `
|
||||
|
||||
10
index.js
10
index.js
@@ -5,6 +5,10 @@ const { client } = require('./app/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;
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
//TODO change password option will be cool
|
||||
@@ -35,12 +39,14 @@ app.post('/auth/signup', async (req, res) => {
|
||||
let password = req.body.password.trim();
|
||||
|
||||
try {
|
||||
// check if user exists
|
||||
const exists = await isUserExists(username);
|
||||
if (exists) {
|
||||
console.log('User already exists');
|
||||
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>");
|
||||
} catch (err) {
|
||||
@@ -49,10 +55,12 @@ 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'));
|
||||
})
|
||||
|
||||
// serving the signup page
|
||||
app.get('/signup', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/public/routes/signup.html'));
|
||||
})
|
||||
@@ -60,7 +68,7 @@ app.get('/signup', (req, res) => {
|
||||
app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, '/public/routes/index.html'));
|
||||
})
|
||||
|
||||
// run server
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
})
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
},
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"crypto": "^1.0.1",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
"express-session": "^1.18.0",
|
||||
|
||||
Reference in New Issue
Block a user