login now checks against db and attaches user data to req.user

This commit is contained in:
Mikayla Dobson
2022-09-28 17:16:51 -05:00
parent 404bc27c57
commit da4fed8e1e
7 changed files with 55 additions and 27 deletions

View File

@@ -64,7 +64,7 @@ async function main() {
description VARCHAR, description VARCHAR,
categoryId INT REFERENCES category(id), categoryId INT REFERENCES category(id),
regionId INT REFERENCES region(id), regionId INT REFERENCES region(id),
price NUMERIC, price MONEY,
unit VARCHAR, unit VARCHAR,
inventory INT inventory INT
); );

View File

@@ -4,7 +4,7 @@ const pgp = require('pg-promise')({ capSQL: true });
module.exports = class CartModel { module.exports = class CartModel {
async create(userid) { async create(userid) {
try { try {
const statement = pgp.helpers.insert(userid, null, 'cart') + 'RETURNING *'; const statement = pgp.helpers.insert({userid: userid}, null, 'cart') + 'RETURNING *';
const result = await db.query(statement); const result = await db.query(statement);
if (result.rows.length) return result.rows[0]; if (result.rows.length) return result.rows[0];
return null; return null;

View File

@@ -1,8 +1,14 @@
const db = require('../db/Pool'); const db = require('../db/Pool');
const pgp = require('pg-promise')({ capSQL: true }); const pgp = require('pg-promise')({ capSQL: true });
const CartModel = require('./CartModel');
const ProductModel = require('./ProductModel');
const CartInstance = new CartModel();
const ProductInstance = new ProductModel();
// TODO: ensure all methods point to the table 'products_carts' rather than 'products_orders'
module.exports = class CartProductModel { module.exports = class CartProductModel {
async create(data) { async _create(data) {
try { try {
const statement = pgp.helpers.insert(data, null, 'products_orders') + 'RETURNING *'; const statement = pgp.helpers.insert(data, null, 'products_orders') + 'RETURNING *';
const result = await db.query(statement); const result = await db.query(statement);
@@ -13,6 +19,18 @@ module.exports = class CartProductModel {
} }
} }
async create(productid) {
try {
const data = ProductInstance.findOne(productid);
const statement = pgp.helpers.insert(data, null, 'products_carts') + 'RETURNING *';
const result = await db.query(statement);
if (result.rows.length) return result.rows[0];
return null;
} catch(e) {
throw new Error(e);
}
}
async find(cartid) { async find(cartid) {
try { try {
const statement = "SELECT * FROM products_orders WHERE cartid = $1"; const statement = "SELECT * FROM products_orders WHERE cartid = $1";

View File

@@ -18,18 +18,17 @@ module.exports = (app, passport) => {
router.post('/login', passport.authenticate('local'), async (req, res, next) => { router.post('/login', passport.authenticate('local'), async (req, res, next) => {
try { try {
const data = req.body; const data = req.body;
const response = await AuthServiceInstance.login(data); const user = await AuthServiceInstance.login(data);
res.status(200).send(response); if (user) req.user = user;
res.status(200).send(user);
} catch(e) { } catch(e) {
next(e); next(e);
} }
}) })
// OAuth2 yet to be implemented
router.get('/google', async (req, res, next) => { router.get('/google', async (req, res, next) => {
try { res.send("google response will go here");
} catch(e) {
next(e);
}
}) })
} }

View File

@@ -5,22 +5,36 @@ const CartServiceInstance = new CartService();
module.exports = (app) => { module.exports = (app) => {
app.use('/api/cart', router); app.use('/api/cart', router);
router.post('/:userId', async (req, res, next) => { // logic for global cart entries
const { userId } = req.params; router.get('/', async (req, res, next) => {
const { id } = req.user;
try { try {
const response = await CartServiceInstance.getCart(userId); const response = await CartServiceInstance.getCart(id);
res.status(200).send(response); res.status(200).send(response);
} catch(e) { } catch(e) {
next(e); next(e);
} }
}) })
router.put('/:userId', async (req, res, next) => { router.post('/', async (req, res, next) => {
const { userId, data } = req.params; const { id } = req.user;
try { try {
const response = await CartServiceInstance.create(id);
res.status(201).send(response);
} catch(e) {
next(e);
}
})
// logic for cart contents
router.post('/items/:itemid', async (req, res, next) => {
const { id } = req.user;
const { itemid } = req.params;
try {
const response = await CartServiceInstance.addItem(id, itemid);
} catch(e) { } catch(e) {
next(e); next(e);
} }

View File

@@ -37,16 +37,9 @@ module.exports = class AuthService {
try { try {
const user = await UserInstance.findOneByEmail(email); const user = await UserInstance.findOneByEmail(email);
if (!user) throw createError(401, 'Incorrect email or password'); if (!user) throw createError(401, 'Incorrect email or password');
// const match = bcrypt.compare(user.password, password, (result, err) => {
// if (err) throw err; const match = bcrypt.compare(password, user.password).then((result) => console.log(result));
// return result; if (!match) throw createError(401, 'Incorrect email or password');
// })
// console.log(match);
// if (!match) throw createError(401, 'Incorrect email or password');
console.log(user.password);
return user; return user;
} catch(e) { } catch(e) {
throw createError(500, e); throw createError(500, e);

View File

@@ -15,11 +15,15 @@ module.exports = class CartService {
async getCart(userid) { async getCart(userid) {
const result = await CartInstance.findOneByUserId(userid); const result = await CartInstance.findOneByUserId(userid);
if (!result) throw createError(404, "Cart not found");
console.log(result.id);
return result;
} }
async addItem(userid, item) { async addItem(userid, itemid) {
const cart = await CartInstance.findOneByUserId(userid); const cart = await CartInstance.findOneByUserId(userid);
const newItem = await CartProductInstance.create(item); const newItem = await CartProductInstance.create(itemid);
} }
async removeItem(userid, item) { async removeItem(userid, item) {