From 9ea8a1756f4f488a3d64046f0489f8260810dc86 Mon Sep 17 00:00:00 2001 From: Cody Seibert Date: Thu, 17 Oct 2024 10:27:13 -0400 Subject: [PATCH] only auth users can submit maps --- convex/maps.ts | 18 ++++++++++++++---- convex/schema.ts | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/convex/maps.ts b/convex/maps.ts index 80d0b8f..e404171 100644 --- a/convex/maps.ts +++ b/convex/maps.ts @@ -9,6 +9,8 @@ import { v } from "convex/values"; import { ZombieSurvival } from "../simulators/zombie-survival"; import { api, internal } from "./_generated/api"; import { runModel } from "../models"; +import { auth } from "./auth"; +import { getAuthUserId } from "@convex-dev/auth/server"; const LEVELS = [ { @@ -58,13 +60,21 @@ export const addMap = mutation({ args: { grid: v.array(v.array(v.string())), }, - handler: async(ctx, args)=>{ - const arr = ctx.db.query("maps").collect(); + handler: async (ctx, args) => { + const userId = await getAuthUserId(ctx); + + if (!userId) { + throw new Error("User not authenticated"); + } + + const maps = await ctx.db.query("maps").collect(); + await ctx.db.insert("maps", { - level: (await arr).length+1, + level: maps.length + 1, grid: args.grid, + submittedBy: userId, }); - } + }, }); export const seedMaps = internalMutation({ diff --git a/convex/schema.ts b/convex/schema.ts index 04eaf23..6ec248c 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -14,6 +14,7 @@ export default defineSchema({ maps: defineTable({ level: v.number(), grid: v.array(v.array(v.string())), + submittedBy: v.optional(v.id("users")), }).index("by_level", ["level"]), scores: defineTable({ modelId: v.string(),