tRPCttRPC
Powered by
MugetsuM
tRPC•3y ago
Mugetsu

Express tRPC NextJS

I've custom expressjs server with Nextjs + im using internal package for handling OneLogin (OIDC) hooked as express middleware which runs. before the Nextjs trpc. I encounter a problem where I do fire a request api call via trpc query or mutation and the OIDC middleware catches that the request is 401 trpc throws on me that
TRPCClientError: Unexpected token 'U', "Unauthorized" is not valid JSON
TRPCClientError: Unexpected token 'U', "Unauthorized" is not valid JSON


if I change the response of the middleware as a json instead of text something like

app.use((req, res, next) => {
  if (/* some condition checking for the cookie */) {
    next();
  } else {
    // Respond with a 401 status and a JSON body
    res.status(401).json({ error: "Unauthorized" });
  }
});
app.use((req, res, next) => {
  if (/* some condition checking for the cookie */) {
    next();
  } else {
    // Respond with a 401 status and a JSON body
    res.status(401).json({ error: "Unauthorized" });
  }
});


Then I get
app-index.js:31 TRPCClientError: Unexpected non-whitespace character after JSON at position 14 (line 1 column 15)
app-index.js:31 TRPCClientError: Unexpected non-whitespace character after JSON at position 14 (line 1 column 15)
with tRPC. What do i need to do to handle the 401 via trpc and be able to redirect the user to login "client side redirect at this point"
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Express.JS and tRPC
xDr.Ma[L]eKXxDr.Ma[L]eK / ❓-help
2y ago
tRPC + Express + Open Telemetry
ItsWendellIItsWendell / ❓-help
3y ago
express-session for tRPC
ippoIippo / ❓-help
3y ago
Express + tRPC: handle express errors from client
ReadorRReador / ❓-help
3y ago