Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: show actionable SSO profile guidance

Richie McIlroy73ae7dc8214f
diff --git a/apps/web/__tests__/unit/sso-login-pages.test.ts b/apps/web/__tests__/unit/sso-login-pages.test.ts
index 531a623f3..339124803 100644
--- a/apps/web/__tests__/unit/sso-login-pages.test.ts
+++ b/apps/web/__tests__/unit/sso-login-pages.test.ts
@@ -1,4 +1,4 @@
-import { createElement } from "react";
+import { type ButtonHTMLAttributes, createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
import LoginPage from "@/app/(org)/login/page";
@@ -6,6 +6,7 @@ import SignupPage from "@/app/(org)/signup/page";
const mocks = vi.hoisted(() => ({
getCurrentUser: vi.fn(),
+ searchParams: new URLSearchParams(),
redirect: vi.fn((url: string): never => {
throw new Error(`redirect:${url}`);
}),
@@ -17,7 +18,35 @@ vi.mock("@cap/database/auth/session", () => ({
vi.mock("@cap/env", () => ({
serverEnv: () => ({ WEB_URL: "https://cap.test" }),
}));
-vi.mock("next/navigation", () => ({ redirect: mocks.redirect }));
+vi.mock("next/navigation", () => ({
+ redirect: mocks.redirect,
+ useSearchParams: () => mocks.searchParams,
+ useRouter: () => ({ push: vi.fn() }),
+}));
+vi.mock("@cap/ui", () => ({
+ Button: ({
+ spinner: _spinner,
+ ...props
+ }: ButtonHTMLAttributes<HTMLButtonElement> & { spinner?: boolean }) =>
+ createElement("button", { type: "button", ...props }),
+ Input: "input",
+ LogoBadge: "div",
+}));
+vi.mock("@/actions/organization/get-organization-sso-data", () => ({
+ getOrganizationSSOData: vi.fn(),
+}));
+vi.mock("@/app/(org)/auth-email", () => ({
+ getEmailCodeCooldownSeconds: () => 0,
+ requestEmailCode: vi.fn(),
+}));
+vi.mock("@/app/utils/analytics", () => ({ trackEvent: vi.fn() }));
+vi.mock("@/utils/public-env", () => ({
+ usePublicEnv: () => ({
+ googleAuthAvailable: false,
+ workosAuthAvailable: true,
+ }),
+}));
+vi.mock("next-auth/react", () => ({ signIn: vi.fn() }));
vi.mock("@/app/(org)/login/form", () => ({
LoginForm: () => createElement("form", { "aria-label": "Login form" }),
}));
@@ -34,6 +63,7 @@ const ssoEntries: [string, Query][] = [
["work-domain entry", { sso: "1" }],
["expired intent retry", { error: "SsoSessionExpired" }],
["failed SSO retry", { error: "SsoSignInFailed" }],
+ ["missing profile retry", { error: "SsoMissingProfileAttributes" }],
[
"unapproved domain retry",
{ error: "profile_not_allowed_outside_organization" },
@@ -43,6 +73,7 @@ const ssoEntries: [string, Query][] = [
beforeEach(() => {
mocks.getCurrentUser.mockResolvedValue({ id: "existing-user" });
+ mocks.searchParams = new URLSearchParams();
});
describe.each([
@@ -137,3 +168,50 @@ describe("authenticated login continuation", () => {
).rejects.toThrow("redirect:/dashboard");
});
});
+
+describe.each(["login", "signup"] as const)(
+ "%s missing-profile notice",
+ (surface) => {
+ async function renderForm() {
+ const Form =
+ surface === "login"
+ ? (
+ await vi.importActual<typeof import("@/app/(org)/login/form")>(
+ "@/app/(org)/login/form",
+ )
+ ).LoginForm
+ : (
+ await vi.importActual<typeof import("@/app/(org)/signup/form")>(
+ "@/app/(org)/signup/form",
+ )
+ ).SignupForm;
+ return renderToStaticMarkup(createElement(Form));
+ }
+
+ it("renders actionable guidance and the SSO form before client effects run", async () => {
+ mocks.searchParams.set("error", "SsoMissingProfileAttributes");
+
+ const markup = await renderForm();
+
+ expect(markup).toContain('role="alert"');
+ expect(markup).toContain("SSO profile details are missing");
+ expect(markup).toContain("first name, and last name");
+ expect(markup).toContain("IT administrator");
+ expect(markup).toContain("SSO attribute mapping");
+ expect(markup).toContain('name="organization"');
+ expect(markup).toContain("Continue with SSO");
+ });
+
+ it.each(["", "SsoSignInFailed", "<script>untrusted error</script>"])(
+ "does not show a missing-profile notice for %j",
+ async (error) => {
+ mocks.searchParams.set("error", error);
+
+ const markup = await renderForm();
+
+ expect(markup).not.toContain("SSO profile details are missing");
+ expect(markup).not.toContain("untrusted error");
+ },
+ );
+ },
+);
diff --git a/apps/web/app/(org)/login/form.tsx b/apps/web/app/(org)/login/form.tsx
index 68eba9c6b..f68643c74 100644
--- a/apps/web/app/(org)/login/form.tsx
+++ b/apps/web/app/(org)/login/form.tsx
@@ -29,6 +29,7 @@ import { trackEvent } from "@/app/utils/analytics";
import { usePublicEnv } from "@/utils/public-env";
import { getEmailCodeCooldownSeconds, requestEmailCode } from "../auth-email";
import { getSafeNextPath } from "../safe-next";
+import { SsoErrorNotice } from "../sso-error-notice";
const MotionInput = motion(Input);
const MotionLogoBadge = motion(LogoBadge);
@@ -47,6 +48,7 @@ export function LoginForm() {
Boolean(
searchParams?.get("organizationId") ||
searchParams?.get("connection_id") ||
+ searchParams?.get("error") === "SsoMissingProfileAttributes" ||
searchParams?.get("sso") === "1" ||
searchParams?.get("mobileProvider") === "workos",
),
@@ -91,6 +93,9 @@ export function LoginForm() {
return toast.error(
"This email already has a Cap account. Sign in using your original sign-in method.",
);
+ } else if (error === "SsoMissingProfileAttributes") {
+ setShowOrgInput(true);
+ return;
} else if (error === "SsoSessionExpired") {
setShowOrgInput(true);
return toast.error(
@@ -308,6 +313,7 @@ export function LoginForm() {
</motion.p>
</motion.div>
<motion.div layout="position" className="flex flex-col space-y-3">
+ <SsoErrorNotice error={searchParams?.get("error")} />
<Suspense
fallback={
<>
diff --git a/apps/web/app/(org)/login/page.tsx b/apps/web/app/(org)/login/page.tsx
index f98e51377..4be90a993 100644
--- a/apps/web/app/(org)/login/page.tsx
+++ b/apps/web/app/(org)/login/page.tsx
@@ -37,6 +37,7 @@ export default async function LoginPage(props: {
sso === "1" ||
error === "SsoSessionExpired" ||
error === "SsoSignInFailed" ||
+ error === "SsoMissingProfileAttributes" ||
error === "profile_not_allowed_outside_organization" ||
error === "signin_consent_denied",
);
diff --git a/apps/web/app/(org)/signup/form.tsx b/apps/web/app/(org)/signup/form.tsx
index 61f444760..5ede56750 100644
--- a/apps/web/app/(org)/signup/form.tsx
+++ b/apps/web/app/(org)/signup/form.tsx
@@ -29,6 +29,7 @@ import { trackEvent } from "@/app/utils/analytics";
import { usePublicEnv } from "@/utils/public-env";
import { getEmailCodeCooldownSeconds, requestEmailCode } from "../auth-email";
import { getSafeNextPath } from "../safe-next";
+import { SsoErrorNotice } from "../sso-error-notice";
const MotionInput = motion(Input);
const MotionLogoBadge = motion(LogoBadge);
@@ -47,6 +48,7 @@ export function SignupForm() {
Boolean(
searchParams?.get("organizationId") ||
searchParams?.get("connection_id") ||
+ searchParams?.get("error") === "SsoMissingProfileAttributes" ||
searchParams?.get("sso") === "1",
),
);
@@ -88,6 +90,9 @@ export function SignupForm() {
return toast.error(
"This email already has a Cap account. Sign in using your original sign-in method.",
);
+ } else if (error === "SsoMissingProfileAttributes") {
+ setShowOrgInput(true);
+ return;
} else if (error === "SsoSessionExpired") {
setShowOrgInput(true);
return toast.error(
@@ -271,6 +276,7 @@ export function SignupForm() {
</motion.p>
</motion.div>
<motion.div layout="position" className="flex flex-col space-y-3">
+ <SsoErrorNotice error={searchParams?.get("error")} />
<Suspense
fallback={
<>
diff --git a/apps/web/app/(org)/signup/page.tsx b/apps/web/app/(org)/signup/page.tsx
index dd570c327..bd46ed15e 100644
--- a/apps/web/app/(org)/signup/page.tsx
+++ b/apps/web/app/(org)/signup/page.tsx
@@ -35,6 +35,7 @@ export default async function SignupPage(props: {
sso === "1" ||
error === "SsoSessionExpired" ||
error === "SsoSignInFailed" ||
+ error === "SsoMissingProfileAttributes" ||
error === "profile_not_allowed_outside_organization" ||
error === "signin_consent_denied",
);
diff --git a/apps/web/app/(org)/sso-error-notice.tsx b/apps/web/app/(org)/sso-error-notice.tsx
new file mode 100644
index 000000000..2e0522910
--- /dev/null
+++ b/apps/web/app/(org)/sso-error-notice.tsx
@@ -0,0 +1,26 @@
+export function SsoErrorNotice({
+ error,
+}: {
+ error: string | null | undefined;
+}) {
+ if (error !== "SsoMissingProfileAttributes") return null;
+
+ return (
+ <div
+ role="alert"
+ className="rounded-xl border border-red-200 bg-red-50 p-4 text-sm text-red-700"
+ >
+ <h2 className="mb-2 font-semibold">SSO profile details are missing</h2>
+ <p>
+ Your identity provider did not send all the profile details needed to
+ sign in. Check that your profile includes your email, first name, and
+ last name.
+ </p>
+ <p className="mt-2">
+ If those are filled in, ask your IT administrator to check the required
+ SSO attribute mapping. After correcting the profile or mapping, start
+ SSO again.
+ </p>
+ </div>
+ );
+}