I'm having an issue where my tRPC-configuration cannot access the express session on the request object.
I am using passport.js with google and facebook providers, and on any normal http-route (not on the tRPC router), I get the userinfo when calling req.user
.
app.ts:
import * as trpc from '@trpc/server';
import * as trpcExpress from '@trpc/server/adapters/express';
const appRouter = trpc
.router()
.mutation('addTodo', {
input: z.string(),
resolve: ({input, ctx}) => {
// Add a todo
},
});
const app = express();
app.use(
session({
secret: 'use an env-variable here',
}),
);
app.use(passport.initialize());
app.use(passport.session());
app.use(
'/trpc',
trpcExpress.createExpressMiddleware({
router: appRouter,
createContext: (ctx: trpcExpress.CreateExpressContextOptions) => {
// === HERE LIES THE ISSUE ===
console.log(ctx.req.user);
// ^ THIS RETURNS UNDEFINED
return ctx;
},
}),
);
app.get("ping", (req, res) => {
console.log(req.user);
// ^ THIS RETURNS THE USER
res.send("pong");
})
It would be easy to say that tRPC doesn't support give you the user, but there must be some sort of workaround, right?
I added this to my client-side fetcher used by trpc
{
credentials: "include"
}