more experiments with connected back end structure
This commit is contained in:
@@ -5,6 +5,7 @@ const pool = new Pool({ connectionString: process.env.CONNECTION });
|
||||
|
||||
module.exports = {
|
||||
// text = SQL query; params = array of values to inject
|
||||
pool,
|
||||
connect: async () => await pool.connect().then(console.log("Connection successful.")),
|
||||
query: (text, params) => pool.query(text, params),
|
||||
end: async () => await pool.end()
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
require('dotenv').config();
|
||||
const cors = require('cors');
|
||||
const { pool } = require('../db/Pool');
|
||||
const session = require('express-session');
|
||||
const { json, urlencoded } = require('express');
|
||||
|
||||
module.exports = (app) => {
|
||||
app.use(cors());
|
||||
|
||||
app.use(json());
|
||||
|
||||
app.use(urlencoded({
|
||||
extended: true
|
||||
}));
|
||||
|
||||
app.use(require('../routes/API'));
|
||||
|
||||
app.use(session({
|
||||
secret: process.env.EXPRESS_SECRET,
|
||||
cookie: { maxAge: 8 * 60 * 60 * 1000, secure: false },
|
||||
resave: false,
|
||||
saveUninitialized: true,
|
||||
store: new (require('connect-pg-simple')(session))({
|
||||
pool: pool,
|
||||
createTableIfMissing: true,
|
||||
pruneSessionInterval: 60 * 30
|
||||
})
|
||||
}));
|
||||
|
||||
// app.use(session({
|
||||
// secret: process.env.EXPRESS_SECRET,
|
||||
// cookie: { maxAge: 8*60*60*1000, secure: false },
|
||||
|
||||
@@ -4,4 +4,6 @@ const expressLoader = require('./express');
|
||||
module.exports = async (app) => {
|
||||
// const passport = await passportLoader(app);
|
||||
const express = await expressLoader(app);
|
||||
const passport = await passportLoader(express);
|
||||
|
||||
}
|
||||
@@ -1,25 +1,25 @@
|
||||
const passport = require('passport');
|
||||
const LocalStrategy = require('passport-local');
|
||||
const { connect } = require('../db/Pool');
|
||||
|
||||
module.exports = (app) => {
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
// passport.serializeUser((user, done) => {
|
||||
// done(null, user.id);
|
||||
// });
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user.id);
|
||||
});
|
||||
|
||||
// passport.deserializeUser((id, done) => {
|
||||
// done(null, { id });
|
||||
// });
|
||||
passport.deserializeUser((id, done) => {
|
||||
done(null, { id });
|
||||
});
|
||||
|
||||
/***
|
||||
** TO DO: FINISH CONFIGURING LOCAL STRATEGY
|
||||
***/
|
||||
|
||||
app.use(new LocalStrategy(async (email, password, done) => {
|
||||
const newClient = client();
|
||||
const account = await newClient.query("SELECT * FROM users WHERE email = ($1)", [email])
|
||||
const client = await connect();
|
||||
const account = await client.query("SELECT * FROM users WHERE email = ($1)", [email])
|
||||
}));
|
||||
|
||||
***/
|
||||
}
|
||||
@@ -1,35 +1,15 @@
|
||||
const loginRouter = require('express').Router();
|
||||
const { connect } = require('../db/Pool');
|
||||
const { LoginService } = require('../services/Auth');
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
loginRouter.route('/').post(async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
const client = await connect();
|
||||
|
||||
loginRouter.post('/', passport.authenticate('local'), async (req, res, next) => {
|
||||
try {
|
||||
let hash = await client.query("SELECT password FROM users WHERE email = ($1)", [email]);
|
||||
hash = hash.rows[0].password;
|
||||
|
||||
const match = bcrypt.compare(password, hash);
|
||||
|
||||
if (!match) res.status(403).json({ msg: "Login unsuccessful. Please try again" });
|
||||
if (match) {
|
||||
req.session.authenticated = true;
|
||||
req.session.user = { email: email, password: password }
|
||||
|
||||
let fullUserProfile = await client.query("SELECT * FROM users WHERE email = ($1)", [email]);
|
||||
|
||||
res.send({
|
||||
session: req.session,
|
||||
userProfile: fullUserProfile.rows[0]
|
||||
});
|
||||
}
|
||||
const data = req.body;
|
||||
const response = await LoginService(data);
|
||||
if (response) res.status(200).send(response);
|
||||
} catch(e) {
|
||||
await client.query("ROLLBACK");
|
||||
throw new Error(e);
|
||||
} finally {
|
||||
client.release()
|
||||
console.log("Client disconnected.");
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
34
services/Auth.js
Normal file
34
services/Auth.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const { connect } = require('../db/Pool');
|
||||
|
||||
async function LoginService(data) {
|
||||
const { email, password } = data;
|
||||
const client = await connect();
|
||||
|
||||
try {
|
||||
let hash = await client.query("SELECT password FROM users WHERE email = ($1)", [email]);
|
||||
hash = hash.rows[0].password;
|
||||
|
||||
const match = bcrypt.compare(password, hash);
|
||||
|
||||
if (!match) res.status(403).json({ msg: "Login unsuccessful. Please try again" });
|
||||
if (match) {
|
||||
req.session.authenticated = true;
|
||||
req.session.user = { email: email, password: password }
|
||||
|
||||
let fullUserProfile = await client.query("SELECT * FROM users WHERE email = ($1)", [email]);
|
||||
|
||||
res.send({
|
||||
session: req.session,
|
||||
userProfile: fullUserProfile.rows[0]
|
||||
});
|
||||
}
|
||||
} catch(e) {
|
||||
await client.query("ROLLBACK");
|
||||
throw new Error(e);
|
||||
} finally {
|
||||
client.release()
|
||||
console.log("Client disconnected.");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { LoginService }
|
||||
Reference in New Issue
Block a user