prop
prop15mo ago

cookies not being set in procedure

This is all the code for setting my cookie
import { publicProcedure, router, userProcedure } from '$lib/trpc/t'

export const user = router({
testCookie: publicProcedure
.query(async ({ ctx: { cookies } }) => {
cookies.set("name", "value", {
path: '/'
})
})
})
import { publicProcedure, router, userProcedure } from '$lib/trpc/t'

export const user = router({
testCookie: publicProcedure
.query(async ({ ctx: { cookies } }) => {
cookies.set("name", "value", {
path: '/'
})
})
})
I'm using svelte-kit, but this doesn't set the cookie
5 Replies
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
you probably need to set it on the response object
prop
prop15mo ago
@alex / KATT yeah, how do I get access to that in the context?
Alex / KATT 🐱
Alex / KATT 🐱15mo ago
you can pass it in your createContext-fn
export async function createContext(
opts: trpcNext.CreateNextContextOptions,
) {
return { req: opts.req, res: opts.res }
}
export async function createContext(
opts: trpcNext.CreateNextContextOptions,
) {
return { req: opts.req, res: opts.res }
}
prop
prop15mo ago
@alex / KATT I don’t have access to opts, can this be because I’m using trpc/sveltekit? This is how my createContext looks like
import type { RequestEvent } from '@sveltejs/kit';
import type { inferAsyncReturnType } from '@trpc/server';
import { prisma } from '$lib/prisma';

export async function createContext(event: RequestEvent) {
let cookies = event?.cookies
return {
cookies,
prisma
};
}

export type Context = inferAsyncReturnType<typeof createContext>;
import type { RequestEvent } from '@sveltejs/kit';
import type { inferAsyncReturnType } from '@trpc/server';
import { prisma } from '$lib/prisma';

export async function createContext(event: RequestEvent) {
let cookies = event?.cookies
return {
cookies,
prisma
};
}

export type Context = inferAsyncReturnType<typeof createContext>;
prop
prop15mo ago
GitHub
Unable to delete cookie through client · Issue #78 · icflorescu/trp...
Describe the bug I'm unable to delete a cookie if a procedure is called through the tRPC client (works fine in callers though). To Reproduce // lib/trpc/router.ts import type { RequestEvent } f...