Implementing iron-session with TRPC #765
Replies: 10 comments
-
@Sealis04 Thank you, would you be able to create a very simple GitHub repository where I can Are you running this locally? HTTP or HTTPS? |
Beta Was this translation helpful? Give feedback.
-
This is reassuring at least, let's see why in some other cases it doesn't. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/Sealis04/testironsession Made two ways to change the session here, one is just the submit in home which goes to the procedure made with TRPC, the one that doesn't save the session. The other is with a separate route from TRPC, just visit /setSession. Also, made /getSession to check if the session values change (which doesn't). This was with HTTP, as for sessionOptions it's just these ones
|
Beta Was this translation helpful? Give feedback.
-
I am having the same problem, following |
Beta Was this translation helpful? Give feedback.
-
Was there any solution to this? I am attempting to run |
Beta Was this translation helpful? Give feedback.
-
I have a working example of this. I have a special export const loginProcedure = t.procedure.use(
t.middleware(async ({ ctx, next }) => {
const result = await next({ ctx });
const resultCtx = "ctx" in result ? (result.ctx as Context) : undefined;
if (resultCtx?.sessionUser) {
// resultCtx is not the ctx passed to `responseMeta` where we need
// `_session` to set the cookie. We would do it all in `responseMeta` but
// it is not and awaitable unfortunately.
ctx._session = await sealData(
{
email: resultCtx.sessionUser.email,
id: resultCtx.sessionUser.id,
} satisfies SessionUser,
{ password: ctx.env.SECRET, ttl: 60 * 60 * 24 * 365 }, // 1 year
);
}
return result;
}),
); Then my responseMeta({ ctx, paths, errors }) {
const allOk = errors.length === 0;
if (
allOk &&
ctx?._session &&
paths?.find((path) =>
["auth.setPassword", "auth.login", "auth.verifyOtpCode"].includes(
path,
),
)
) {
return {
headers: {
"set-cookie": `__session=${
ctx._session
}; Max-Age=2592000; SameSite=Strict; Path=/; ${
ENV === "development" ? "" : "Secure; "
}HttpOnly`,
},
};
}
return {};
} Not the most elegant way to set cookies, but it works. |
Beta Was this translation helpful? Give feedback.
-
Hey to whoever is having this problem, simply add this to Gist here |
Beta Was this translation helpful? Give feedback.
-
I am facing the exact same issue. |
Beta Was this translation helpful? Give feedback.
-
for anyone that still have the problem, maybe you can try my solution. after trying so many ways, and of course bcoz i'm too lazy to understand the TRPC concept, at the end i choose to use Server Action for doing mutation the session (save and destroy) it is very easy to implement rather than trying to understand the whole TRPC concept and React Query (if you use t3 like me). the cons is you have to manually setup all the necessary React Query stuff to get the benefit of using react query itself like onSuccess, onError, isPending, etc. or if you're beginner in React Query and you're in hurry just manually setup hooks for handing state or callback after success and error. you can watch this video from Lama Dev, if you don't know how to Server Action. |
Beta Was this translation helpful? Give feedback.
-
The solution outlined here: #765 is the correct one. As with many tooling/frameworks interfacing with Next.js, a big issue is that cookies are not properly forwarded down to the library that needs to read them. So you always have to find the correct way to do so with either trpc or apollo. Good luck! |
Beta Was this translation helpful? Give feedback.
-
So I'm having issues trying to slam iron-session with TRPC. I'm using create-t3-app to start with and before v8, I just used to define (or redefine) IronSession when creating my TRPC context. I assumed V8 was pretty much the same but for some reason, running session.save() inside my procedures doesn't save anything. I tried creating a separate route from trpc to try and see if I can save runs properly, and yes it works. And yes, I can access the session values inside my procedures (but save doesn't work). Any ideas why?
My TRPC Context
My procedure (from create-t3 boilerplate)
Edit*
To add, I already tried calling getIronSession inside the procedure instead. Still didn't work.
Beta Was this translation helpful? Give feedback.
All reactions