feat: improve structure and error handling
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user