From 5b1216f6ed273e78a28880b5738bf3692ae10d55 Mon Sep 17 00:00:00 2001 From: Max Stevens <38756142+maxstevens-nl@users.noreply.github.com> Date: Fri, 27 Mar 2026 11:08:23 +0100 Subject: [PATCH] fix: in example, pass userID via context param on Zero instance (#217) --- playground/src/zero.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/playground/src/zero.ts b/playground/src/zero.ts index b94ab9c..8881572 100644 --- a/playground/src/zero.ts +++ b/playground/src/zero.ts @@ -15,7 +15,7 @@ import { schema, zql } from '~/db/schema' const cookies = useCookies() export interface ZeroContext { - sub: string + userID: string } declare module '@rocicorp/zero' { @@ -33,9 +33,9 @@ export const mutators = defineMutators({ id: z.string(), timestamp: z.number(), }), - async ({ tx, ctx, args: { mediumID, body, id, timestamp } }) => { + async ({ tx, ctx: { userID }, args: { mediumID, body, id, timestamp } }) => { return tx.mutate.message.insert({ - senderID: ctx.sub, + senderID: userID, mediumID, body, id, @@ -45,7 +45,7 @@ export const mutators = defineMutators({ ), update: defineMutator( z.object({ id: z.string(), body: z.string() }), - async ({ tx, ctx, args: { id, body } }) => { + async ({ tx, ctx: { userID }, args: { id, body } }) => { const messageToEdit = await tx.run( zql.message.where('id', id).one(), ) @@ -53,7 +53,7 @@ export const mutators = defineMutators({ throw new Error(`Message with id ${id} not found`) } - if (messageToEdit.senderID !== ctx.sub) { + if (messageToEdit.senderID !== userID) { throw new Error(`You aren't allowed to edit this message`) } @@ -62,8 +62,8 @@ export const mutators = defineMutators({ ), delete: defineMutator( z.object({ id: z.string() }), - async ({ tx, ctx, args: { id } }) => { - if (!ctx.sub) { + async ({ tx, ctx: { userID }, args: { id } }) => { + if (!userID) { throw new Error('You must be logged in to delete') } @@ -115,6 +115,9 @@ export const { useZero, useQuery } = createZeroComposables(() => { return { userID, + context: { + userID, + }, // auth: () => encodedJWT || undefined, server: import.meta.env.VITE_PUBLIC_ZERO_CACHE_URL, schema, -- 2.51.2