splitted code to separate files and folder
This commit is contained in:
17
app/db.js
Normal file
17
app/db.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const {Client} = require('pg');
|
||||
require('dotenv').config();
|
||||
|
||||
const client = new Client({
|
||||
user: process.env.PG_USER,
|
||||
password: process.env.PG_PASSWORD,
|
||||
database: process.env.PG_DATABASE,
|
||||
host: process.env.PG_HOST,
|
||||
port: process.env.PG_PORT
|
||||
});
|
||||
|
||||
// create connection to database
|
||||
client.connect()
|
||||
.then(() => console.log('Successfully connected to database'))
|
||||
.catch((err) => console.log('Error on connecting to database: ', err));
|
||||
|
||||
module.export = client;
|
||||
40
index.js
40
index.js
@@ -1,8 +1,7 @@
|
||||
const pg = require('pg');
|
||||
const express = require('express');
|
||||
const session = require('express-session');
|
||||
const path = require('path');
|
||||
const {Client} = require('pg');
|
||||
const client = require('./app/db')
|
||||
const port = 3000
|
||||
const app = express()
|
||||
require('dotenv').config()
|
||||
@@ -18,43 +17,29 @@ app.use(session({
|
||||
saveUninitialized: true
|
||||
}));
|
||||
|
||||
const client = new Client({
|
||||
user: process.env.PG_USER,
|
||||
password: process.env.PG_PASSWORD,
|
||||
database: process.env.PG_DATABASE,
|
||||
host: process.env.PG_HOST,
|
||||
port: process.env.PG_PORT
|
||||
});
|
||||
|
||||
//create connection to database
|
||||
|
||||
client.connect()
|
||||
.then(() => console.log('Successfully connected to database'))
|
||||
.catch((err) => console.log('Error on connecting to database: ', err));
|
||||
|
||||
|
||||
|
||||
app.post('/auth', function(req, res) {
|
||||
app.post('/auth', async function(req, res) {
|
||||
let username = req.body.username.trim();
|
||||
let password = req.body.password.trim();
|
||||
|
||||
if (username && password) {
|
||||
client.query('SELECT * FROM accounts WHERE username = $1 AND password = $2', [username, password], function(error, results, fields) {
|
||||
if (error) throw error;
|
||||
if (results.length > 0) {
|
||||
//Authenticate the user
|
||||
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
|
||||
req.session.loggedin = true;
|
||||
req.session.username = username;
|
||||
res.redirect('/');
|
||||
} else {
|
||||
res.send('Incorrect Username or Password!');
|
||||
}
|
||||
res.end();
|
||||
});
|
||||
} 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();
|
||||
}
|
||||
res.end();
|
||||
});
|
||||
|
||||
app.get('/login', (req, res) => {
|
||||
@@ -67,5 +52,4 @@ app.get('/', (req, res) => {
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
})
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user