Skip to content

Commit e6e1df6

Browse files
committed
require accountId for all authenticated queries
1 parent 7414c9a commit e6e1df6

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

app/routes/board.$id/queries.ts

+33-11
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { prisma } from "~/db/prisma";
22

33
import { ItemMutation } from "./types";
44

5-
export function deleteCard(id: string) {
6-
return prisma.item.delete({ where: { id } });
5+
export function deleteCard(id: string, accountId: string) {
6+
return prisma.item.delete({ where: { id, Board: { accountId } } });
77
}
88

9-
export async function getBoardData(boardId: number) {
9+
export async function getBoardData(boardId: number, accountId: string) {
1010
return prisma.board.findUnique({
1111
where: {
1212
id: boardId,
13+
accountId: accountId,
1314
},
1415
include: {
1516
items: true,
@@ -18,31 +19,52 @@ export async function getBoardData(boardId: number) {
1819
});
1920
}
2021

21-
export async function updateBoardName(boardId: number, name: string) {
22+
export async function updateBoardName(
23+
boardId: number,
24+
name: string,
25+
accountId: string,
26+
) {
2227
return prisma.board.update({
23-
where: { id: boardId },
28+
where: { id: boardId, accountId: accountId },
2429
data: { name },
2530
});
2631
}
2732

28-
export function upsertItem(mutation: ItemMutation & { boardId: number }) {
33+
export function upsertItem(
34+
mutation: ItemMutation & { boardId: number },
35+
accountId: string,
36+
) {
2937
return prisma.item.upsert({
30-
where: { id: mutation.id },
38+
where: {
39+
id: mutation.id,
40+
Board: {
41+
accountId,
42+
},
43+
},
3144
create: mutation,
3245
update: mutation,
3346
});
3447
}
3548

36-
export async function updateColumnName(id: string, name: string) {
49+
export async function updateColumnName(
50+
id: string,
51+
name: string,
52+
accountId: string,
53+
) {
3754
return prisma.column.update({
38-
where: { id },
55+
where: { id, Board: { accountId } },
3956
data: { name },
4057
});
4158
}
4259

43-
export async function createColumn(boardId: number, name: string, id: string) {
60+
export async function createColumn(
61+
boardId: number,
62+
name: string,
63+
id: string,
64+
accountId: string,
65+
) {
4466
let columnCount = await prisma.column.count({
45-
where: { boardId },
67+
where: { boardId, Board: { accountId } },
4668
});
4769
return prisma.column.create({
4870
data: {

app/routes/board.$id/route.tsx

+8-7
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import {
1818
import { Board } from "./board";
1919

2020
export async function loader({ request, params }: LoaderFunctionArgs) {
21-
await requireAuthCookie(request);
21+
let accountId = await requireAuthCookie(request);
2222

2323
invariant(params.id, "Missing board ID");
2424
let id = Number(params.id);
2525

26-
let board = await getBoardData(id);
26+
let board = await getBoardData(id, accountId);
2727
if (!board) throw notFound();
2828

2929
return { board };
@@ -36,6 +36,7 @@ export const meta: MetaFunction<typeof loader> = ({ data }) => {
3636
export { Board as default };
3737

3838
export async function action({ request, params }: ActionFunctionArgs) {
39+
let accountId = await requireAuthCookie(request);
3940
let boardId = Number(params.id);
4041
invariant(boardId, "Missing boardId");
4142

@@ -47,32 +48,32 @@ export async function action({ request, params }: ActionFunctionArgs) {
4748
switch (intent) {
4849
case INTENTS.deleteCard: {
4950
let id = String(formData.get("itemId") || "");
50-
await deleteCard(id);
51+
await deleteCard(id, accountId);
5152
break;
5253
}
5354
case INTENTS.updateBoardName: {
5455
let name = String(formData.get("name") || "");
5556
invariant(name, "Missing name");
56-
await updateBoardName(boardId, name);
57+
await updateBoardName(boardId, name, accountId);
5758
break;
5859
}
5960
case INTENTS.moveItem:
6061
case INTENTS.createItem: {
6162
let mutation = parseItemMutation(formData);
62-
await upsertItem({ ...mutation, boardId });
63+
await upsertItem({ ...mutation, boardId }, accountId);
6364
break;
6465
}
6566
case INTENTS.createColumn: {
6667
let { name, id } = Object.fromEntries(formData);
6768
invariant(name, "Missing name");
6869
invariant(id, "Missing id");
69-
await createColumn(boardId, String(name), String(id));
70+
await createColumn(boardId, String(name), String(id), accountId);
7071
break;
7172
}
7273
case INTENTS.updateColumn: {
7374
let { name, columnId } = Object.fromEntries(formData);
7475
if (!name || !columnId) throw badRequest("Missing name or columnId");
75-
await updateColumnName(String(columnId), String(name));
76+
await updateColumnName(String(columnId), String(name), accountId);
7677
break;
7778
}
7879
default: {

app/routes/home/queries.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { prisma } from "~/db/prisma";
22

3-
export async function deleteBoard(boardId: number) {
3+
export async function deleteBoard(boardId: number, accountId: string) {
44
return prisma.board.delete({
5-
where: { id: boardId },
5+
where: { id: boardId, accountId },
66
});
77
}
88

app/routes/home/route.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ export async function loader({ request }: LoaderFunctionArgs) {
3232
}
3333

3434
export async function action({ request }: ActionFunctionArgs) {
35-
let userId = await requireAuthCookie(request);
35+
let accountId = await requireAuthCookie(request);
3636
let formData = await request.formData();
3737
let intent = String(formData.get("intent"));
3838
switch (intent) {
3939
case INTENTS.createBoard: {
4040
let name = String(formData.get("name") || "");
4141
let color = String(formData.get("color") || "");
4242
if (!name) throw badRequest("Bad request");
43-
let board = await createBoard(userId, name, color);
43+
let board = await createBoard(accountId, name, color);
4444
return redirect(`/board/${board.id}`);
4545
}
4646
case INTENTS.deleteBoard: {
4747
let boardId = formData.get("boardId");
4848
if (!boardId) throw badRequest("Missing boardId");
49-
await deleteBoard(Number(boardId));
49+
await deleteBoard(Number(boardId), accountId);
5050
return { ok: true };
5151
}
5252
default: {

0 commit comments

Comments
 (0)