Skip to content

Commit

Permalink
only auth users can submit maps
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Oct 17, 2024
1 parent e0b5695 commit 9ea8a17
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 14 additions & 4 deletions convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 9ea8a17

Please sign in to comment.