How to set cookies when signing in from server?

I'm using Hono+TRPC for backend (running on port:3000) and react router v7 framework for client (running on port:5173). This is what the sign-in procedure looks like:
signIn: publicProcedure.input(signInSchema).mutation(async ({ input, ctx }) => {
try {
const res = await auth().api.signInEmail({
body: {
email: input.email,
password: input.password,
callbackURL: `${ctx.env.VITE_PUBLIC_APP_URL}/dash`,
},
headers: ctx.honoContext.req.raw.headers,
returnHeaders: true,
});

console.log("sign-in res:", res);

return { status: "success", message: "Sign-in successful" };
} catch (error) {
return {
status: "error",
message: error instanceof Error ? error.message : "Sign-in failed",
};
}
}),
signIn: publicProcedure.input(signInSchema).mutation(async ({ input, ctx }) => {
try {
const res = await auth().api.signInEmail({
body: {
email: input.email,
password: input.password,
callbackURL: `${ctx.env.VITE_PUBLIC_APP_URL}/dash`,
},
headers: ctx.honoContext.req.raw.headers,
returnHeaders: true,
});

console.log("sign-in res:", res);

return { status: "success", message: "Sign-in successful" };
} catch (error) {
return {
status: "error",
message: error instanceof Error ? error.message : "Sign-in failed",
};
}
}),
It validates the credentials and says sign-in successful, but technically i cant sign in as the cookies are not set. how do i set the cookies for each endpoint.
3 Replies
Vlad
Vlad2mo ago
The best solution is to use authClient on the client But if you want do it this way, set the returned headers using the hono context
Dev
DevOP2mo ago
Yeah, ive solved it
Vlad
Vlad2mo ago
You're creating your routes to fully control auth, right? I just have the same set of tools I don't like the thing that when you add a new plugin, a lot of new routes become available

Did you find this page helpful?