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:
However, since the Hono backend is the Better Auth server, I don't have access to
Before this change, I got the user's session on the Start backend like so:
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?
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?