Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User auth enforce #131

Merged
merged 5 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions backend/routes/basketRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import express from "express";
import { Request, Response } from "express";
import Basket, { IBasket } from "../models/basketSchema";
import connectDB from "../connection";
import { ObjectId } from "mongoose";
import { authenticateUser } from "../auth";

const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
const baskets = await Basket.find({});
Expand All @@ -20,7 +20,7 @@ router.get("/", async (req: Request, res: Response) => {
}
});

router.get("/:basketid", async (req: Request, res: Response) => {
router.get("/:basketid", authenticateUser, async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

Expand Down Expand Up @@ -52,7 +52,7 @@ router.get("/:basketid", async (req: Request, res: Response) => {
}
});

router.post("/", async (req: Request, res: Response) => {
router.post("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
console.log("Creating a new basket with data:", req.body);
Expand All @@ -79,7 +79,7 @@ router.post("/", async (req: Request, res: Response) => {
}
});

router.patch("/:id", async (req: Request, res: Response) => {
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
// Get basket ID from URL
const { id } = req.params;
const updatedData: Partial<IBasket> = req.body; // Not a full update, only partial
Expand All @@ -102,7 +102,7 @@ router.patch("/:id", async (req: Request, res: Response) => {
}
});

router.delete("/:id", async (req: Request, res: Response) => {
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
try {
Expand Down
11 changes: 6 additions & 5 deletions backend/routes/groupRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express from "express";
import { Request, Response } from "express";
import Group, { IGroup } from "../models/groupSchema";
import { authenticateUser } from "../auth";
import connectDB from "../connection";

const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
const users = await Group.find({});
Expand All @@ -19,7 +20,7 @@ router.get("/", async (req: Request, res: Response) => {
}
});

router.get("/:groupid", async (req: Request, res: Response) => {
router.get("/:groupid", authenticateUser, async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

Expand Down Expand Up @@ -54,7 +55,7 @@ router.get("/:groupid", async (req: Request, res: Response) => {
}
});

router.post("/", async (req: Request, res: Response) => {
router.post("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
console.log("Creating a new group with data:", req.body);
Expand Down Expand Up @@ -85,7 +86,7 @@ router.post("/", async (req: Request, res: Response) => {
}
});

router.patch("/:id", async (req: Request, res: Response) => {
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
// Get user ID from URL
const { id } = req.params;
const updatedData: Partial<IGroup> = req.body; //Not a full update only partial
Expand All @@ -108,7 +109,7 @@ router.patch("/:id", async (req: Request, res: Response) => {
}
});

router.delete("/:id", async (req: Request, res: Response) => {
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
try {
Expand Down
11 changes: 6 additions & 5 deletions backend/routes/itemRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express from "express";
import { Request, Response } from "express";
import Item, { IItem } from "../models/itemSchema";
import { authenticateUser } from "../auth";
import connectDB from "../connection";

const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
const users = await Item.find({});
Expand All @@ -19,7 +20,7 @@ router.get("/", async (req: Request, res: Response) => {
}
});

router.get("/:itemid", async (req: Request, res: Response) => {
router.get("/:itemid", authenticateUser, async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

Expand Down Expand Up @@ -55,7 +56,7 @@ router.get("/:itemid", async (req: Request, res: Response) => {
}
});

router.post("/", async (req: Request, res: Response) => {
router.post("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
console.log("Creating a new item with data:", req.body);
Expand Down Expand Up @@ -93,7 +94,7 @@ router.post("/", async (req: Request, res: Response) => {
}
});

router.patch("/:id", async (req: Request, res: Response) => {
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
// Get user ID from URL
const { id } = req.params;
const updatedData: Partial<IItem> = req.body; //Not a full update only partial
Expand All @@ -116,7 +117,7 @@ router.patch("/:id", async (req: Request, res: Response) => {
}
});

router.delete("/:id", async (req: Request, res: Response) => {
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
try {
Expand Down
10 changes: 5 additions & 5 deletions backend/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import bcrypt from "bcrypt";
import mongoose from "mongoose";
const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();

try {
Expand All @@ -22,7 +22,7 @@ router.get("/", async (req: Request, res: Response) => {
}
});

router.get("/:userid", async (req: Request, res: Response) => {
router.get("/:userid", authenticateUser, async (req: Request, res: Response) => {
// Ensure the database connection
connectDB();

Expand Down Expand Up @@ -108,7 +108,7 @@ router.post("/", async (req: Request, res: Response) => {
}
});

router.patch("/:id", async (req: Request, res: Response) => {
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
// Get user ID from URL
const { id } = req.params;
Expand All @@ -131,7 +131,7 @@ router.patch("/:id", async (req: Request, res: Response) => {
}
});

router.delete("/:id", async (req: Request, res: Response) => {
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;

Expand All @@ -149,7 +149,7 @@ router.delete("/:id", async (req: Request, res: Response) => {
}
});

router.delete("/:id/remove-friend", async (req: Request, res: Response) => {
router.delete("/:id/remove-friend", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const userId = req.params.id;
const { friendId } = req.body; // Expecting friendId in the request body
Expand Down
32 changes: 29 additions & 3 deletions frontend/lib/deletes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const handleDeleteGroup = async (groupId: string) => {
try {
const response = await fetch(`${vite_backend_url}/groups/${groupId}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
Expand Down Expand Up @@ -46,6 +49,9 @@ export const handleDeleteItem = async (itemId: string) => {
try {
const response = await fetch(`${vite_backend_url}/items/${itemId}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
Expand All @@ -60,6 +66,9 @@ export const handleDeleteBasket = async (basketId: string) => {
try {
const response = await fetch(`${vite_backend_url}/baskets/${basketId}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
if (!response.ok) {
throw new Error(`Error: ${response.statusText}`);
Expand All @@ -76,7 +85,12 @@ export const handleDeleteGroupFromUsers = async (
try {
// Iterate over each userId
for (const userId of userIds) {
const response = await fetch(`${vite_backend_url}/users/${userId}`);
const response = await fetch(`${vite_backend_url}/users/${userId}`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
}
);
if (response.ok) {
const user = await response.json();
const userGroups = user.groups;
Expand All @@ -94,6 +108,7 @@ export const handleDeleteGroupFromUsers = async (
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ groups: updatedGroups }),
},
Expand All @@ -118,7 +133,12 @@ export const handleDeleteBasketFromGroup = async (
basketId: string,
) => {
try {
const response = await fetch(`${vite_backend_url}/groups/${groupId}`);
const response = await fetch(`${vite_backend_url}/groups/${groupId}`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
}
);
if (response.ok) {
const group = await response.json();
const groupBaskets = group.baskets;
Expand All @@ -138,6 +158,7 @@ export const handleDeleteBasketFromGroup = async (
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ baskets: updatedBaskets }),
},
Expand All @@ -159,7 +180,11 @@ export const handleDeleteBasketFromGroup = async (
export const handleDeleteAllItemsInBasket = async (basketId: string) => {
try {
// Fetch all items in the basket
const response = await fetch(`${vite_backend_url}/baskets/${basketId}`);
const response = await fetch(`${vite_backend_url}/baskets/${basketId}`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
});
if (!response.ok) {
throw new Error(`Error fetching items: ${response.statusText}`);
}
Expand Down Expand Up @@ -189,6 +214,7 @@ export const removeFriendFromUserByFriendId = async (
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ friendId: friendId }),
},
Expand Down
16 changes: 16 additions & 0 deletions frontend/lib/edits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const editGroup = async (groupId: string, groupData: updatedGroup) => {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify(groupData),
});
Expand All @@ -57,6 +58,7 @@ export const editBasket = async (
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify(basketData),
});
Expand All @@ -70,6 +72,7 @@ export const addItemToBasket = async (
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ items: basketItems }),
});
Expand All @@ -80,6 +83,7 @@ export const editItem = async (itemId: string, itemData: updatedItem) => {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify(itemData),
});
Expand Down Expand Up @@ -155,6 +159,7 @@ export const editUser = async (
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify(userData),
});
Expand All @@ -181,3 +186,14 @@ export const addUserToGroup = async (group: IGroup, users: ObjectId[]) => {
body: JSON.stringify({ members: users }),
});
};

export const addFriendToUser = async (user: IUser, updatedFriends: ObjectId[]) => {
return fetch(`${vite_backend_url}/users/${user._id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ friends: updatedFriends }),
});
}
Loading
Loading