fix(login): Add default organization fallback for IDP user creation #11025 (#11026)

# Which Problems Are Solved

When users authenticate via IDP (Identity Provider) without explicit
organization context, the flow could fail or create users without proper
organization assignment. This occurred when:

- No organization parameter was provided in the IDP callback
- Domain discovery didn't find a matching organization
- OIDC requests didn't include organization scopes

# How the Problems Are Solved

Implemented a fallback mechanism that ensures organization context is
always available:

- Centralized organization resolution in `resolveOrganizationForUser()`
  - First: Use explicitly provided organization
  - Second: Attempt domain discovery from username
  - Third: Fallback to default organization (NEW)
- Explicit error handling: Users are never created without organization
context. If no organization can be determined (including no default
org), the flow fails gracefully with a clear error message.
- Applied to both creation flows:
  - CASE 4: Auto-creation of users
  - CASE 5: Manual user registration

Co-authored-by: Ramon <[email protected]>
This commit is contained in:
Max Peintner
2025-11-04 08:58:41 +01:00
committed by GitHub
co-authored by Ramon
parent ee17b7a074
commit 66e04b1dad
2 changed files with 63 additions and 18 deletions
+19 -14
View File
@@ -10,6 +10,7 @@ import {
addHuman,
getLoginSettings,
getOrgsByDomain,
getDefaultOrg,
} from "@/lib/zitadel";
import { headers } from "next/headers";
import { create } from "@zitadel/client";
@@ -56,7 +57,10 @@ async function resolveOrganizationForUser({
}
}
}
return undefined;
// Fallback to default organization if no org was resolved through discovery
const defaultOrg = await getDefaultOrg({ serviceUrl });
return defaultOrg?.id;
}
/**
@@ -333,20 +337,21 @@ export async function processIDPCallback({
serviceUrl,
});
let addHumanUserWithOrganization: AddHumanUserRequest;
if (orgToRegisterOn) {
const organizationSchema = create(OrganizationSchema, {
org: { case: "orgId", value: orgToRegisterOn },
});
addHumanUserWithOrganization = create(AddHumanUserRequestSchema, {
...addHumanUser,
organization: organizationSchema,
});
} else {
addHumanUserWithOrganization = create(AddHumanUserRequestSchema, addHumanUser);
if (!orgToRegisterOn) {
console.error("[IDP Process] Could not determine organization for auto-creation (no default org available)");
const params = buildRedirectParams();
return { redirect: `/idp/${provider}/failure?${params}&error=no_organization_context` };
}
const organizationSchema = create(OrganizationSchema, {
org: { case: "orgId", value: orgToRegisterOn },
});
const addHumanUserWithOrganization = create(AddHumanUserRequestSchema, {
...addHumanUser,
organization: organizationSchema,
});
try {
const newUser = await addHuman({
serviceUrl,
@@ -394,7 +399,7 @@ export async function processIDPCallback({
});
if (!orgToRegisterOn) {
console.error("[IDP Process] Could not determine organization for registration");
console.error("[IDP Process] Could not determine organization for registration (no default org available)");
const params = buildRedirectParams();
return { redirect: `/idp/${provider}/registration-failed?${params}` };
}