Skip to content

Commit 5b1216f

Browse files
fix: in example, pass userID via context param on Zero instance (#217)
1 parent 14ed211 commit 5b1216f

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

playground/src/zero.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { schema, zql } from '~/db/schema'
1515
const cookies = useCookies()
1616

1717
export interface ZeroContext {
18-
sub: string
18+
userID: string
1919
}
2020

2121
declare module '@rocicorp/zero' {
@@ -33,9 +33,9 @@ export const mutators = defineMutators({
3333
id: z.string(),
3434
timestamp: z.number(),
3535
}),
36-
async ({ tx, ctx, args: { mediumID, body, id, timestamp } }) => {
36+
async ({ tx, ctx: { userID }, args: { mediumID, body, id, timestamp } }) => {
3737
return tx.mutate.message.insert({
38-
senderID: ctx.sub,
38+
senderID: userID,
3939
mediumID,
4040
body,
4141
id,
@@ -45,15 +45,15 @@ export const mutators = defineMutators({
4545
),
4646
update: defineMutator(
4747
z.object({ id: z.string(), body: z.string() }),
48-
async ({ tx, ctx, args: { id, body } }) => {
48+
async ({ tx, ctx: { userID }, args: { id, body } }) => {
4949
const messageToEdit = await tx.run(
5050
zql.message.where('id', id).one(),
5151
)
5252
if (!messageToEdit) {
5353
throw new Error(`Message with id ${id} not found`)
5454
}
5555

56-
if (messageToEdit.senderID !== ctx.sub) {
56+
if (messageToEdit.senderID !== userID) {
5757
throw new Error(`You aren't allowed to edit this message`)
5858
}
5959

@@ -62,8 +62,8 @@ export const mutators = defineMutators({
6262
),
6363
delete: defineMutator(
6464
z.object({ id: z.string() }),
65-
async ({ tx, ctx, args: { id } }) => {
66-
if (!ctx.sub) {
65+
async ({ tx, ctx: { userID }, args: { id } }) => {
66+
if (!userID) {
6767
throw new Error('You must be logged in to delete')
6868
}
6969

@@ -115,6 +115,9 @@ export const { useZero, useQuery } = createZeroComposables(() => {
115115

116116
return {
117117
userID,
118+
context: {
119+
userID,
120+
},
118121
// auth: () => encodedJWT || undefined,
119122
server: import.meta.env.VITE_PUBLIC_ZERO_CACHE_URL,
120123
schema,

0 commit comments

Comments
 (0)