Liltripple_reid
Liltripple_reid14mo ago

`createCaller` and RSC - with next-auth

I've been upgrading my trpc routers and handlers to support edge+app router with drizzle and next-auth; and something that has come up is the use of createCaller (with createTRPCNext instead of createTRPCReact - and without the TRPCProvider). So I wanted to ask if this is a correct way of using that function
// app/_trpc/server-client.tsx
import { type Session } from "next-auth";
import { appRouter } from "~/server/api/root";

export const serverClient = ({ session }: { session: Session | null }) => {
return appRouter.createCaller({ session });
};
// app/_trpc/server-client.tsx
import { type Session } from "next-auth";
import { appRouter } from "~/server/api/root";

export const serverClient = ({ session }: { session: Session | null }) => {
return appRouter.createCaller({ session });
};
4 Replies
Liltripple_reid
Liltripple_reid14mo ago
so with that I'm using it in RSC pages like this
import { getServerSession } from "next-auth";
import { authOptions } from "~/server/auth";
import { serverClient } from "./_trpc/server-client";

export default async function LandingPage() {
const session = await getServerSession(authOptions);
const data = await serverClient({ session }).example.allUsers();

return (
<section>
<h1>Landing Page</h1>

<pre>{JSON.stringify(data)}</pre>
</section>
);
}
import { getServerSession } from "next-auth";
import { authOptions } from "~/server/auth";
import { serverClient } from "./_trpc/server-client";

export default async function LandingPage() {
const session = await getServerSession(authOptions);
const data = await serverClient({ session }).example.allUsers();

return (
<section>
<h1>Landing Page</h1>

<pre>{JSON.stringify(data)}</pre>
</section>
);
}
I'm wondering if returning/creating the caller on every RSC is a good practice, because I read that it's possible that it may execute all middlewares and validations again
Alex / KATT 🐱
Alex / KATT 🐱14mo ago
It only executes the middlewares on the route you call
Liltripple_reid
Liltripple_reid14mo ago
so it's safe to use like this, right?
Alex / KATT 🐱
Alex / KATT 🐱14mo ago
Yes