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 { 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