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

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

View File

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