feat: improve structure and error handling

This commit is contained in:
Michael Zetterberg
2024-05-14 15:55:46 +02:00
parent 01587d7fd5
commit f5108d1a8e
104 changed files with 1505 additions and 1570 deletions

View File

@@ -3,7 +3,7 @@ import { AuthError } from "next-auth"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
import { badRequest, internalServerError } from "@/server/errors/next"
import { internalServerError } from "@/server/errors/next"
import { signIn } from "@/auth"
@@ -22,17 +22,15 @@ export async function GET(
// Normal login request from New web
redirectTo =
request.cookies.get("redirectTo")?.value || // Cookie gets set by authRequired middleware
request.headers.get("x-redirect-to") ||
request.nextUrl.searchParams.get("redirectTo") ||
""
"/"
// If above fails, always redirect to startpage
if (!redirectTo) {
// Make relative URL to absolute URL
if (redirectTo.startsWith("/")) {
if (!env.PUBLIC_URL) {
throw internalServerError("No value for env.PUBLIC_URL")
}
redirectTo = env.PUBLIC_URL
console.log({ login_fallback: redirectTo })
redirectTo = new URL(redirectTo, env.PUBLIC_URL).href
}
// Clean up cookie from authRequired middleware
@@ -104,11 +102,11 @@ export async function GET(
}
} catch (error) {
if (error instanceof AuthError) {
console.log({ signInAuthError: error })
console.error({ signInAuthError: error })
} else {
console.log({ signInError: error })
console.error({ signInError: error })
}
}
return badRequest()
return internalServerError()
}