34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
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 } |