NekoChan
NekoChan
TTCTheo's Typesafe Cult
Created by Mainak on 9/15/2024 in #questions
useQuery vs useSuspenseQuery
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
and you can use cookies in tRPC context, for example:
signIn: publicProcedure
.input(credentialSchema)
.mutation(async ({ ctx, input }) => {
...

ctx.cookies.set(SESSION_TOKEN_COOKIE, sessionToken, {
expires: sessionTokenExpires,
path: '/',
})

...
}),
signIn: publicProcedure
.input(credentialSchema)
.mutation(async ({ ctx, input }) => {
...

ctx.cookies.set(SESSION_TOKEN_COOKIE, sessionToken, {
expires: sessionTokenExpires,
path: '/',
})

...
}),
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
3. update createTRPCContext and createInnerTRPCContext functions ( I'm using cookie: https://www.npmjs.com/package/cookie )
// src\server\api\trpc.ts
export const createTRPCContext = async (opts: {
req: NextRequest
resHeaders: Headers ⬅️⬅️⬅️⬅️⬅️
}) => {
return await createInnerTRPCContext({
headers: opts.req.headers,
resHeaders: opts.resHeaders, ⬅️⬅️⬅️⬅️⬅️
})
}

export const createInnerTRPCContext = async (opts: CreateContextOptions) => {
const serverAuthSession = await getServerAuthSession()
⬇️⬇️⬇️⬇️⬇️
const cookies = {
get: (name?: string) => {
const cookiesHeader = opts.headers.get('Cookie')
if (!cookiesHeader) return null
const cookies = parse(cookiesHeader)
return name ? cookies[name] ?? null : cookies
},
has: (name: string) => {
const cookiesHeader = opts.headers.get('Cookie')
if (!cookiesHeader) return false
const cookies = parse(cookiesHeader)
return name in cookies
},
set: (name: string, value: string, options?: CookieSerializeOptions) => {
opts.resHeaders.append('Set-Cookie', serialize(name, value, options))
},
clear: (name: string) => {
opts.resHeaders.append('Set-Cookie', serialize(name, '', { maxAge: -1 }))
},
}

return {
session: serverAuthSession,
headers: opts.headers,
cookies: cookies, ⬅️⬅️⬅️⬅️⬅️
db,
}
}
// src\server\api\trpc.ts
export const createTRPCContext = async (opts: {
req: NextRequest
resHeaders: Headers ⬅️⬅️⬅️⬅️⬅️
}) => {
return await createInnerTRPCContext({
headers: opts.req.headers,
resHeaders: opts.resHeaders, ⬅️⬅️⬅️⬅️⬅️
})
}

export const createInnerTRPCContext = async (opts: CreateContextOptions) => {
const serverAuthSession = await getServerAuthSession()
⬇️⬇️⬇️⬇️⬇️
const cookies = {
get: (name?: string) => {
const cookiesHeader = opts.headers.get('Cookie')
if (!cookiesHeader) return null
const cookies = parse(cookiesHeader)
return name ? cookies[name] ?? null : cookies
},
has: (name: string) => {
const cookiesHeader = opts.headers.get('Cookie')
if (!cookiesHeader) return false
const cookies = parse(cookiesHeader)
return name in cookies
},
set: (name: string, value: string, options?: CookieSerializeOptions) => {
opts.resHeaders.append('Set-Cookie', serialize(name, value, options))
},
clear: (name: string) => {
opts.resHeaders.append('Set-Cookie', serialize(name, '', { maxAge: -1 }))
},
}

return {
session: serverAuthSession,
headers: opts.headers,
cookies: cookies, ⬅️⬅️⬅️⬅️⬅️
db,
}
}
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
2. pass resHeader to createTRPCContext function
// src\app\api\trpc\[trpc]\route.ts
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'
import type { NextRequest } from 'next/server'

import { env } from '~/env.mjs'
import { appRouter } from '~/server/api/root'
import { createTRPCContext } from '~/server/api/trpc'

const handler = (req: NextRequest) =>
fetchRequestHandler({
endpoint: '/api/trpc',
req,
router: appRouter,
createContext: ({ resHeaders }) => createTRPCContext({ req, resHeaders }), ⬅️⬅️⬅️⬅️⬅️
onError:
env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(
`❌ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`,
)
}
: undefined,
})

export { handler as GET, handler as POST }
// src\app\api\trpc\[trpc]\route.ts
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'
import type { NextRequest } from 'next/server'

import { env } from '~/env.mjs'
import { appRouter } from '~/server/api/root'
import { createTRPCContext } from '~/server/api/trpc'

const handler = (req: NextRequest) =>
fetchRequestHandler({
endpoint: '/api/trpc',
req,
router: appRouter,
createContext: ({ resHeaders }) => createTRPCContext({ req, resHeaders }), ⬅️⬅️⬅️⬅️⬅️
onError:
env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(
`❌ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`,
)
}
: undefined,
})

export { handler as GET, handler as POST }
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
1. change unstable_httpBatchStreamLink to httpBatchLink in trpc/react.tsx and trpc/server.ts ( https://discord.com/channels/966627436387266600/966627439390380064/1165106604128092190 )
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
I found the solution.
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
No description
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
No description
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 10/20/2023 in #questions
How to set cookie in ct3 app router?
No description
12 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/30/2023 in #questions
Clerk middleware breaks Discord URL preview (embed)
it's only happen in Discord, Telegram and Twitter are fine.
9 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/30/2023 in #questions
Clerk middleware breaks Discord URL preview (embed)
my middleware.ts looks like this
import { authMiddleware } from '@clerk/nextjs/server'

export default authMiddleware({
publicRoutes: ['/:path*'],
})

export const config = {
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
}
import { authMiddleware } from '@clerk/nextjs/server'

export default authMiddleware({
publicRoutes: ['/:path*'],
})

export const config = {
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
}
9 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/30/2023 in #questions
Clerk middleware breaks Discord URL preview (embed)
9 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/30/2023 in #questions
Clerk middleware breaks Discord URL preview (embed)
( with Clerk middleware ) https://blog.nekochantw.xyz/api/og
9 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/30/2023 in #questions
Clerk middleware breaks Discord URL preview (embed)
the og image api preview also not working
9 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/30/2023 in #questions
Clerk middleware breaks Discord URL preview (embed)
9 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/30/2023 in #questions
Clerk middleware breaks Discord URL preview (embed)
9 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/11/2023 in #questions
dose anyone know why my meta tags are not working...?
I tried to change my domain: https://neko-blog.vercel.app/
6 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/11/2023 in #questions
dose anyone know why my meta tags are not working...?
6 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/11/2023 in #questions
dose anyone know why my meta tags are not working...?
6 replies
TTCTheo's Typesafe Cult
Created by NekoChan on 5/11/2023 in #questions
dose anyone know why my meta tags are not working...?
i'm using next-seo
6 replies