more experiments with connected back end structure

This commit is contained in:
Mikayla Dobson
2022-07-05 12:30:06 -05:00
parent f1d964b092
commit 20d704851f
6 changed files with 66 additions and 38 deletions

34
services/Auth.js Normal file
View 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 }