code refactor

This commit is contained in:
slawk0
2024-08-20 23:22:20 +02:00
parent 9af2ebf3a2
commit ea55f80f3c
2 changed files with 28 additions and 26 deletions

View File

@@ -15,7 +15,7 @@ client.connect()
.catch((err) => console.log('Error on connecting to database: ', err));
// function for checking if user exists
async function isUserExists(username) {
try {
const query = 'SELECT COUNT(*) FROM accounts WHERE username = $1';
@@ -27,7 +27,8 @@ async function isUserExists(username) {
}
}
async function insertAccount(username, password){
// function for putting user data to database
async function insertUser(username, password){
const query = `
INSERT INTO accounts (username, password)
VALUES ($1, $2)
@@ -43,9 +44,30 @@ async function insertAccount(username, password){
}
}
// function for login user
async function loginUser (username, password) {
if (username && password) {
try {
const result = await client.query('SELECT * FROM accounts WHERE username = $1 AND password = $2', [username, password]);
if (result.rows.length > 0) {
res.redirect('/');
} else {
res.send('Incorrect Username or Password!');
}
} catch (error) {
console.error('Error executing query', error);
res.status(500).send('Error executing query');
}
} else {
res.send('Please enter Username and Password!');
}
res.end();
}
module.exports = {
client,
insertAccount,
insertUser,
isUserExists,
loginUser
};

View File

@@ -1,10 +1,10 @@
const express = require('express');
const session = require('express-session');
const path = require('path');
const client = require('./app/db');
const { client } = require('./app/db');
const port = 3000
const app = express()
const { insertAccount, isUserExists } = require('./app/db');
const { insertUser, isUserExists, loginUser } = require('./app/db');
require('dotenv').config()
//TODO change password option will be cool
@@ -27,25 +27,6 @@ app.post('/auth/login', async function(req, res) {
await loginUser(username, password);
async function loginUser (username, password) {
if (username && password) {
try {
const result = await client.query('SELECT * FROM accounts WHERE username = $1 AND password = $2', [username, password]);
if (result.rows.length > 0) {
// Authenticate the user
res.redirect('/');
} else {
res.send('Incorrect Username or Password!');
}
} catch (error) {
console.error('Error executing query', error);
res.status(500).send('Error executing query');
}
} else {
res.send('Please enter Username and Password!');
}
res.end();
}
});
app.post('/auth/signup', async (req, res) => {
@@ -53,7 +34,6 @@ app.post('/auth/signup', async (req, res) => {
let username = req.body.username.trim();
let password = req.body.password.trim();
insertAccount(username, password);
try {
const exists = await isUserExists(username);
if (exists) {
@@ -61,7 +41,7 @@ app.post('/auth/signup', async (req, res) => {
res.status(500).send('User already exists!');
return;
}
await insertAccount(username, password);
await insertUser(username, password);
res.status(200).send("Account successfully created <a href=/login>Login screen</a>");
} catch (err) {
console.error('Error inserting data:', err);