export const createContext = async ({ req, res }: trpcExpress.CreateExpressContextOptions) => {
// include the request and response objects to be used in procedures
return { req, res };
};
export const protectedProcedure = t.procedure.use(async function isAuthenticated(opts) {
const { req } = opts.ctx;
// this will call res.status(401).send("UNAUTHORIZED") if the token is invalid
// but it's not a simple function and i would have to rewrite our whole auth layer to decouple it from the req/res
await runJWTValidations(req, res);
const token = getTokenFromRequest(req);
const user = await getUserFromToken(token);
if (!user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return opts.next({
ctx: { ...opts.ctx, user },
});
});
export const createContext = async ({ req, res }: trpcExpress.CreateExpressContextOptions) => {
// include the request and response objects to be used in procedures
return { req, res };
};
export const protectedProcedure = t.procedure.use(async function isAuthenticated(opts) {
const { req } = opts.ctx;
// this will call res.status(401).send("UNAUTHORIZED") if the token is invalid
// but it's not a simple function and i would have to rewrite our whole auth layer to decouple it from the req/res
await runJWTValidations(req, res);
const token = getTokenFromRequest(req);
const user = await getUserFromToken(token);
if (!user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return opts.next({
ctx: { ...opts.ctx, user },
});
});