tomheaton
tomheaton
TtRPC
Created by ippo on 4/29/2023 in #❓-help
express-session for tRPC
vercel just announced vercel-kv 🙌
9 replies
TtRPC
Created by ippo on 4/29/2023 in #❓-help
express-session for tRPC
you can send the auth cookie or similar with the request in the headers (https://trpc.io/docs/client/setup)
httpBatchLink({
url: 'http://localhost:3000/trpc',
// You can pass any HTTP headers you wish here
async headers() {
return {
authorization: getAuthCookie(),
};
},
}),
httpBatchLink({
url: 'http://localhost:3000/trpc',
// You can pass any HTTP headers you wish here
async headers() {
return {
authorization: getAuthCookie(),
};
},
}),
then define a middleware that uses the auth to find a matching session (in your redis etc.) (https://trpc.io/docs/server/middlewares)
const hasSession = middleware(async (opts) => {
const { req } = opts;

// get session from redis etc.
const session = await getSessionFromRedis(req.headers);

if (!session) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}

return opts.next({
ctx: {
user: session.user,
},
});
});
const hasSession = middleware(async (opts) => {
const { req } = opts;

// get session from redis etc.
const session = await getSessionFromRedis(req.headers);

if (!session) {
throw new TRPCError({ code: 'UNAUTHORIZED' });
}

return opts.next({
ctx: {
user: session.user,
},
});
});
9 replies
TtRPC
Created by outis99 on 10/17/2022 in #❓-help
Can I redirect the user from inside my router?
after raising a trpc error in your router when no item is found
7 replies
TtRPC
Created by outis99 on 10/17/2022 in #❓-help
Can I redirect the user from inside my router?
on your useQuery you can add an onError handler where you could redirect the user
7 replies