bad request error on login route
This commit is contained in:
103
routes/user.js
103
routes/user.js
@@ -1,22 +1,57 @@
|
||||
const express = require('express');
|
||||
const userRouter = express.Router();
|
||||
|
||||
// const client = require('../db/Client');
|
||||
const userRouter = require('express').Router();
|
||||
const { connect } = require('../db/Pool');
|
||||
|
||||
// get a list of all users, or a single user matching an email passed in as a query param
|
||||
userRouter.route('/').get(async (req, res) => {
|
||||
const { email } = req.query;
|
||||
const client = await connect()
|
||||
.then(console.log('Connection successful.'))
|
||||
.catch(e => console.log(e));
|
||||
|
||||
if (!email) {
|
||||
module.exports = (app) => {
|
||||
app.use('/api/user', userRouter);
|
||||
|
||||
// get a list of all users, or a single user matching an email passed in as a query param
|
||||
userRouter.route('/').get(async (req, res) => {
|
||||
const { email } = req.query;
|
||||
const client = await connect()
|
||||
.then(console.log('Connection successful.'))
|
||||
.catch(e => console.log(e));
|
||||
|
||||
if (!email) {
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
const results = await client.query("SELECT * FROM users");
|
||||
await client.query("COMMIT");
|
||||
if (results) res.send(results.rows);
|
||||
} catch(e) {
|
||||
await client.query('ROLLBACK');
|
||||
throw new Error(e);
|
||||
} finally {
|
||||
await client.release();
|
||||
console.log("Client disconnected.");
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
const result = await client.query(("SELECT * FROM users WHERE email = ($1)"), [email])
|
||||
await client.query("COMMIT");
|
||||
if (result) res.send(result.rows);
|
||||
} catch(e) {
|
||||
await client.query('ROLLBACK');
|
||||
throw new Error(e);
|
||||
} finally {
|
||||
await client.release();
|
||||
console.log("Client disconnected.");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// post a new user to the database
|
||||
userRouter.route('/').post(async (req, res) => {
|
||||
const { name, email } = req.body;
|
||||
const client = await connect()
|
||||
.then(console.log('Connection successful.'));
|
||||
const input = "INSERT INTO users (name, email) VALUES ($1, $2)";
|
||||
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
const results = await client.query("SELECT * FROM users");
|
||||
await client.query(input, [name, email]);
|
||||
await client.query("COMMIT");
|
||||
if (results) res.send(results.rows);
|
||||
res.sendStatus(200);
|
||||
} catch(e) {
|
||||
await client.query('ROLLBACK');
|
||||
throw new Error(e);
|
||||
@@ -24,41 +59,5 @@ userRouter.route('/').get(async (req, res) => {
|
||||
await client.release();
|
||||
console.log("Client disconnected.");
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
const result = await client.query(("SELECT * FROM users WHERE email = ($1)"), [email])
|
||||
await client.query("COMMIT");
|
||||
if (result) res.send(result.rows);
|
||||
} catch(e) {
|
||||
await client.query('ROLLBACK');
|
||||
throw new Error(e);
|
||||
} finally {
|
||||
await client.release();
|
||||
console.log("Client disconnected.");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// post a new user to the database
|
||||
userRouter.route('/').post(async (req, res) => {
|
||||
const { name, email } = req.body;
|
||||
const client = await connect()
|
||||
.then(console.log('Connection successful.'));
|
||||
const input = "INSERT INTO users (name, email) VALUES ($1, $2)";
|
||||
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
await client.query(input, [name, email]);
|
||||
await client.query("COMMIT");
|
||||
res.sendStatus(200);
|
||||
} catch(e) {
|
||||
await client.query('ROLLBACK');
|
||||
throw new Error(e);
|
||||
} finally {
|
||||
await client.release();
|
||||
console.log("Client disconnected.");
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = userRouter;
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user