infodusha
infodusha6mo ago

Hot to redirect in middleware when using createCallerFactory

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
5 Replies
Nick
Nick6mo ago
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)
infodusha
infodusha6mo ago
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
Nick
Nick6mo ago
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
infodusha
infodusha6mo ago
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)
More 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: nod