app.post('/login', (request, response) => {
// retrieve the user entry using their email
const values = [request.body.email];
pool.query('SELECT * from users WHERE email=$1', values, (error, result) => {
// return if there is a query error
console.log('Error executing query', error.stack);
response.status(503).send(result.rows);
// we didnt find a user with that email
if (result.rows.length === 0) {
// the error for incorrect email and incorrect password are the same for security reasons.
// This is to prevent detection of whether a user has an account for a given service.
response.status(403).send('login failed!');
// get user record from results
const user = result.rows[0];
const shaObj = new jsSHA('SHA-512', 'TEXT', { encoding: 'UTF8' });
// input the password from the request to the SHA object
shaObj.update(request.body.password);
// get the hashed value as output from the SHA object
const hashedPassword = shaObj.getHash('HEX');
// If the user's hashed password in the database does not match the hashed input password, login fails
if (user.password !== hashedPassword) {
// the error for incorrect email and incorrect password are the same for security reasons.
// This is to prevent detection of whether a user has an account for a given service.
response.status(403).send('login failed!');
// The user's password hash matches that in the DB and we authenticate the user.
response.cookie('loggedIn', true);
response.send('logged in!');