I'm encountering an issue with my tRPC configuration where it is unable to access the express session on the request object.
Currently, I am implementing passport.js with Google and Facebook providers. Whenever I make a request to a regular HTTP route (outside of the tRPC router), I am able to retrieve the user info by calling req.user
.
Here is a snippet from my app.ts file:
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) => {
// === ISSUE OCCURS HERE ===
console.log(ctx.req.user);
// ^ RETURNS UNDEFINED
return ctx;
},
}),
);
app.get("ping", (req, res) => {
console.log(req.user);
// ^ RETURNS THE USER
res.send("pong");
})
While it may seem like tRPC does not provide user information, there might be a workaround available. Any suggestions on how to address this challenge?