Getting session from external server on a different server

Basically I have two backends here: one for TanStack Start's SSR, and one for Better Auth and the site's API (using Hono).

Before this change, I got the user's session on the Start backend like so:
import { createServerFn } from "@tanstack/react-start";
import { getWebRequest } from "@tanstack/react-start/server";
import { auth } from "~/auth";

const getUser = createServerFn({ method: "GET" }).handler(async () => {
    const { headers } = getWebRequest()!;
    const session = await auth.api.getSession({ headers });
    return session?.user || null;
});

However, since the Hono backend is the Better Auth server, I don't have access to auth. Is there a way to do this on the Start backend?
Solution
//middleware to get the user and session and add them to the context
app.use("*", async (c, next) => {
const session = await auth.api.getSession({ headers: c.req.raw.headers });

if (!session) {
c.set("user", null);
c.set("session", null);
return next();
}

c.set("user", session.user);
c.set("session", session.session);
return next();
});

In your case you would replace auth.api.getSession with the authClient.getSession

Make sense?
Was this page helpful?