more experiments with connected back end structure
This commit is contained in:
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user