Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(auth): render IdP callback error message on sign-in page (#10169)

Marc Klingen2e507fdf8f68
diff --git a/web/src/pages/api/auth/[...nextauth].ts b/web/src/pages/api/auth/[...nextauth].ts
index 8a62ed0ae8..7747a3f1d2 100644
--- a/web/src/pages/api/auth/[...nextauth].ts
+++ b/web/src/pages/api/auth/[...nextauth].ts
@@ -1,4 +1,5 @@
import { getAuthOptions } from "@/src/server/auth";
+import { env } from "@/src/env.mjs";
import type { NextApiRequest, NextApiResponse } from "next";
import NextAuth from "next-auth";
@@ -8,6 +9,34 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "HEAD") {
return res.status(200).end();
}
+
+ // Intercept OAuth callback errors to preserve error_description from IdP
+ // This happens before NextAuth processes the callback, allowing us to preserve
+ // the IdP's error_description which NextAuth would otherwise strip
+ // Only intercept if this is an OAuth callback request (path starts with 'callback')
+ const isCallbackRequest =
+ Array.isArray(req.query.nextauth) &&
+ req.query.nextauth.length > 0 &&
+ req.query.nextauth[0] === "callback";
+
+ if (
+ isCallbackRequest &&
+ req.query.error &&
+ req.query.error_description &&
+ typeof req.query.error === "string" &&
+ typeof req.query.error_description === "string"
+ ) {
+ const error = req.query.error;
+ const errorDescription = req.query.error_description;
+ const basePath = env.NEXT_PUBLIC_BASE_PATH ?? "";
+
+ // Redirect directly to sign-in with error and error_description preserved
+ // This bypasses NextAuth's error page which strips error_description
+ return res.redirect(
+ `${basePath}/auth/sign-in?error=${encodeURIComponent(error)}&error_description=${encodeURIComponent(errorDescription)}`,
+ );
+ }
+
// Do whatever you want here, before the request is passed down to `NextAuth`
const authOptions = await getAuthOptions();
// https://github.com/nextauthjs/next-auth/issues/2408#issuecomment-1382629234
diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsx
index 53294465e4..ccfd346962 100644
--- a/web/src/pages/auth/sign-in.tsx
+++ b/web/src/pages/auth/sign-in.tsx
@@ -507,19 +507,32 @@ export default function SignIn({
typeof router.query.error === "string"
? decodeURIComponent(router.query.error)
: null;
- const nextAuthErrorDescription = signInErrors.find(
- (e) => e.code === nextAuthError,
- )?.description;
+ const nextAuthErrorDescription =
+ typeof router.query.error_description === "string"
+ ? decodeURIComponent(router.query.error_description)
+ : null;
+
+ // Use error_description from IdP if available, otherwise use mapped error or error code
+ const errorMessage = nextAuthErrorDescription
+ ? nextAuthErrorDescription
+ : (signInErrors.find((e) => e.code === nextAuthError)?.description ??
+ nextAuthError);
+
useEffect(() => {
// log unexpected sign in errors to Sentry
- if (nextAuthError && !nextAuthErrorDescription) {
+ // An error is unexpected if it's not in our mapped errors and has no IdP error_description
+ if (
+ nextAuthError &&
+ !nextAuthErrorDescription &&
+ !signInErrors.find((e) => e.code === nextAuthError)
+ ) {
captureException(new Error(`Sign in error: ${nextAuthError}`));
}
}, [nextAuthError, nextAuthErrorDescription]);
const [credentialsFormError, setCredentialsFormError] = useState<
string | null
- >(nextAuthErrorDescription ?? nextAuthError);
+ >(errorMessage);
// Two-step login flow: ask for email first, detect SSO, then either redirect to SSO or reveal password field.
// Skip this flow when no SSO is configured - show password field immediately
const [showPasswordStep, setShowPasswordStep] = useState<boolean>(