Replies: 4 comments 3 replies
-
Thanks! I also found this solution and created these functions to interact with cookies. You'll have to also pass the request through the context for npm install cookie
npm install -D @types/cookie import cookie, { CookieSerializeOptions } from 'cookie'
export function getCookies(req: Request) {
const cookieHeader = req.headers.get('Cookie')
if (!cookieHeader) return {}
return cookie.parse(cookieHeader)
}
export function getCookie(req: Request, name: string) {
const cookieHeader = req.headers.get('Cookie')
if (!cookieHeader) return
const cookies = cookie.parse(cookieHeader)
return cookies[name]
}
export function setCookie(
resHeaders: Headers,
name: string,
value: string,
options?: CookieSerializeOptions
) {
resHeaders.append('Set-Cookie', cookie.serialize(name, value, options))
} Now you can set/get cookies similar to how getCookie(ctx.req, 'access_token')
setCookie(
ctx.resHeaders,
'access_token',
access_token,
accessTokenCookieOptions
) |
Beta Was this translation helpful? Give feedback.
2 replies
-
i love you both for this this 100% works and i didnt think it would be this straightforward |
Beta Was this translation helpful? Give feedback.
0 replies
-
export const createTRPCContext = (
_opts: CreateNextContextOptions,
) => {
return {
..._opts,
db,
};
// return createInnerTRPCContext({},_opts);
}; . const token = signJWT(opts.input);
opts.ctx.res.setHeader("Set-Cookie", `myCookie=${token}; HttpOnly`); Why cant we do like this? |
Beta Was this translation helpful? Give feedback.
1 reply
-
If its |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was looking to set some cookies after a login executed via a procedure. The procedure creates a new session. By default you can not do it within a procedure. I've checked the docs and did not find a solution - maybe its somewhere, but i did not find it.
So I've found this way myself. Maybe its useful.
Simply pass the resHeaders as context:
Now you can use it in your procedures:
You obviously need to refresh the page client-side on success to resend it to the backend in case you have server-side (next13/rsc) authorization via session cookie.
Example in next13:
Beta Was this translation helpful? Give feedback.
All reactions