fix: refactor onError

This commit is contained in:
Christel Westerberg
2024-05-30 15:31:23 +02:00
parent 66da53925e
commit 83a0609b1a

View File

@@ -1,10 +1,8 @@
import { TRPCError } from "@trpc/server" import { TRPCError } from "@trpc/server"
import { redirect } from "next/navigation" import { redirect } from "next/navigation"
import { NextResponse } from "next/server"
import { Lang } from "@/constants/languages" import { Lang } from "@/constants/languages"
import { webviews } from "@/constants/routes/webviews" import { webviews } from "@/constants/routes/webviews"
import { env } from "@/env/server"
import { appRouter } from "@/server" import { appRouter } from "@/server"
import { createContext } from "@/server/context" import { createContext } from "@/server/context"
import { internalServerError } from "@/server/errors/next" import { internalServerError } from "@/server/errors/next"
@@ -24,15 +22,20 @@ export function serverClient() {
if (error instanceof TRPCError) { if (error instanceof TRPCError) {
if (error.code === "UNAUTHORIZED") { if (error.code === "UNAUTHORIZED") {
const lang = ctx?.lang || Lang.en let lang = Lang.en
let pathname = "/"
let fullUrl = "/"
const langIndex = ctx!.url.indexOf(`/${lang}`) if (ctx) {
const pathname = ctx?.url.substring(langIndex) lang = ctx.lang
pathname = ctx.pathname
fullUrl = ctx.url
}
if (pathname && webviews.includes(pathname)) { const fullPathname = new URL(fullUrl).pathname
const returnUrl = ctx!.url
const redirectUrl = `/${lang}/webview/refresh?returnurl=${encodeURIComponent(returnUrl)}` if (webviews.includes(fullPathname)) {
const redirectUrl = `/${lang}/webview/refresh?returnurl=${encodeURIComponent(fullUrl)}`
console.error( console.error(
"Unautorized in webview, redirecting to: ", "Unautorized in webview, redirecting to: ",
redirectUrl redirectUrl