T
tRPC

❓-help

Hot to redirect in middleware when using createCallerFactory

Iinfodusha2/9/2024
Hello. I do have this tiny middleware:
const authMiddleware = middleware(async (opts) => {
const result = await opts.next();
if (!result.ok) {
if (result.error.status === ErrorCode.TOKEN_INVALID) {
redirect('/logout');
}
}
return result;
});
const authMiddleware = middleware(async (opts) => {
const result = await opts.next();
if (!result.ok) {
if (result.error.status === ErrorCode.TOKEN_INVALID) {
redirect('/logout');
}
}
return result;
});
I am aware of redirect throws an error but it seems to be wrapped in a result.error.cause when thrown out so next can't realize that is redirect error and just ouputs Error: NEXT_REDIRECT to console. isRedirectError(result.error) === false while isRedirectError(result.error.cause) === true Is there any known workaround for this to work? Node 20 / npm 10
Nnlucas2/9/2024
This appears to be tRPC working as designed I don’t believe it’s possible to throw a redirect with NextJS APIs from within tRPC You’d need to catch an error and handle it (I can be wrong as I’m not a next user)
Iinfodusha2/9/2024
You’d need to catch an error and handle it
I wish to, the thing is there is no way I can handle it because the error thrown is the handler itself
Nnlucas2/9/2024
You’d throw a bespoke error and then handle that I believe, you can’t use next APIs in tRPC any more than you can use AngularJs in React
Iinfodusha2/9/2024
I imagine this as a flag / API to tell tRPC do not wrap that particular error in TrpcError that i am not aware of Ahh I see your point here A have also tried
const redirectError = getRedirectError('/logout', RedirectType.push, RedirectStatusCode.TemporaryRedirect);
const trpcError = new TRPCError({
message: redirectError.message,
code: 'UNAUTHORIZED',
cause: redirectError.cause,
});
const magicError = Object.assign(redirectError, trpcError);
throw magicError;
const redirectError = getRedirectError('/logout', RedirectType.push, RedirectStatusCode.TemporaryRedirect);
const trpcError = new TRPCError({
message: redirectError.message,
code: 'UNAUTHORIZED',
cause: redirectError.cause,
});
const magicError = Object.assign(redirectError, trpcError);
throw magicError;
to preted that is TRPC error but aparently it is not thrown but just logged. Now the only solution seems to we wrapping the call in api that will allow to throw outside of caller factory (sounds like making a proxy object or so)

Looking for more? Join the community!

Recommended Posts
Remix: How can I return headers from a query or mutation back to the action/loader?I'm using Supabase Auth and a requirement is to return the headers back from its client creation funUsing output validator breaks query return type inference on react queryHi! I am not sure whether this is expected and whether there is some way to fix that but here it goeHow to pass headers from serverActions in Next.js App router.I'm using tRPC with Clerk auth provider in Next.js App directory. I created a `serverClient` to use Need Guidance Optimizing Multi-Select Typeahead### What your environment is: Node 18? Bun? pnpm / yarn / npm? - Bun v1.0.23 - Next.js v14.1.0 (app Private API, public clientsHey everyone, we're about to OSS some good stuff at my company and I'm wondering what's the best appHow to get the type of context after middleware context extension?I have a middleware where I validate and set a userId. My router knows that userId is no longer undewhat am i doing wrong?My turbo repo containing expo-clerk-trpc does NOT work. and I've tried everything I have no idea whaReusable Component to take router as props / dependency injection. How to type?for example: ```ts export const Content = async ({ api }) => { const items = await api.getAll.quertRPC mutate call firing two requests with custom linkNode: 18.16 tRPC: 10.38.0 pnpm What's wrong with this custom link that's causing it's addition to aIssue with Fastify / tRPC / AngularWe are trying to setup Angular project with tRPC. In Angular we get following error: ``` Error: nodDrizzle query not working only inside trpcMy drizzle query works inside a regular nextjs api route. The same query running inside trpc throws Server freezes when doing mutations...I have the exact same issue as mentioned in this post: https://discord-questions.trpc.io/m/117697170Create Wrapper only for Procedures that support Infinite QueryGreetings, I'm attempting to create a help that will handle some generic logic I desire for a seriWhy am I getting 'Argument type is not assignable to parameter type ProcedureResolver<unknown>'?I have this warning in my editor (jetbrains InteliJ): ``` Argument type () => any[] is not assignabEfficient way to use tRPC client with auth headers from secure storageWondering if anyone has a recommended pattern on caching the deviceId / authHeader using a React ConUsing tRPC with Expo API Routes feature?Is there a possibility to merge "tRPC Express Adapter" with "Expo API Route Express Deployment"? tRTRPC not working on multi tenant appEverything works on localhost. When I deploy it on the vercel None of the mutations work. Every qTRPCClientError: Unable to transform response from serverHi 🙂 I've just started - so nothing more than boiler plate code but for some reason it doesn't workoptimistic updates tRPC v11 + TanStack Query v5I am reading https://tanstack.com/query/latest/docs/framework/react/guides/optimistic-updates#if-theT3 Stack TRPC used in Server ActionsIs there issues using the server calls of TRPC in a nextjs server action? I am seeing weird issues w