infodushaI
tRPC2y ago
12 replies
infodusha

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;
});

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
Was this page helpful?