使用Elysia与TRPC时Response Cookie无法持久化的问题
最近使用Bun搭建了基于Remix + Elysia封装tRPC + Prisma的项目,开发了简易登录系统,计划采用JWT Cookie认证流程,但遇到Cookie无法持久化的问题:尽管响应中存在Cookie,且网络工具可识别,但它不会存入本地存储,后续请求也不会携带该Cookie。
相关网络信息
- Headers(附截图)
- Cookies(附截图)
- Stored cookies(附截图)
Elysia 配置代码
const CONFIG = { host: '127.0.0.1', port: 3000, protocol: 'http' } new Elysia() .use( cors({ credentials: true, origin: true, } )) .use( trpc( router, { createContext, } ) ) .listen({ hostname: CONFIG.host, port: CONFIG.port, }) console.log(`listening on http://${CONFIG.host}:${CONFIG.port}`)
createContext 实现代码
export async function createContext(opts: FetchCreateContextFnOptions) { return opts }
tRPC 登录查询中设置Cookie的代码
ctx.resHeaders.set( 'Set-Cookie', 'hello=world; SameSite=Lax; Path=/; Max-Age=3600; HttpOnly' )
内容的提问来源于stack exchange,提问作者alexjkni.dev
相关产品推荐
相关产品推荐

