Mugetsu
Mugetsu15mo ago

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 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) 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"
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?