Good way to handle redirect on unauthorized request response

Hello, I want to redirect to landing page when any of the auth checked endpoints return 401. Currently I've landed on a solution where I throw a specific error when response status is 401, then check the error message on the fetch hook. This solution requires every service to implement that if check, feels kind of clunky. Any suggestions?
2 Replies
Fursum
Fursum16mo ago
redirect function uses useRouter under useUnauthorized, also resets the cookies with useCookie
Yiannis
Yiannis16mo ago
you can do it in the query if using trpc. after input in the options object there is the isError function which takes in the error. You can check there and use the router to push to a different page i also do it like this: if (cocktails.error?.data?.code === "UNAUTHORIZED") { void router.push("/"); }