From 83a0609b1a9fe1b3d3f123974293bab03bb40f58 Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Thu, 30 May 2024 15:31:23 +0200 Subject: [PATCH] fix: refactor onError --- lib/trpc/server.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/trpc/server.ts b/lib/trpc/server.ts index e3bf0bcb5..a6467fb91 100644 --- a/lib/trpc/server.ts +++ b/lib/trpc/server.ts @@ -1,10 +1,8 @@ import { TRPCError } from "@trpc/server" import { redirect } from "next/navigation" -import { NextResponse } from "next/server" import { Lang } from "@/constants/languages" import { webviews } from "@/constants/routes/webviews" -import { env } from "@/env/server" import { appRouter } from "@/server" import { createContext } from "@/server/context" import { internalServerError } from "@/server/errors/next" @@ -24,15 +22,20 @@ export function serverClient() { if (error instanceof TRPCError) { if (error.code === "UNAUTHORIZED") { - const lang = ctx?.lang || Lang.en + let lang = Lang.en + let pathname = "/" + let fullUrl = "/" - const langIndex = ctx!.url.indexOf(`/${lang}`) - const pathname = ctx?.url.substring(langIndex) + if (ctx) { + lang = ctx.lang + pathname = ctx.pathname + fullUrl = ctx.url + } - if (pathname && webviews.includes(pathname)) { - const returnUrl = ctx!.url + const fullPathname = new URL(fullUrl).pathname - const redirectUrl = `/${lang}/webview/refresh?returnurl=${encodeURIComponent(returnUrl)}` + if (webviews.includes(fullPathname)) { + const redirectUrl = `/${lang}/webview/refresh?returnurl=${encodeURIComponent(fullUrl)}` console.error( "Unautorized in webview, redirecting to: ", redirectUrl