find org context from loginname

This commit is contained in:
peintnermax
2024-08-26 09:26:23 +02:00
parent 7d4603dce9
commit 2f843621bf
2 changed files with 34 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ import { idpTypeToSlug } from "@/lib/idp";
import {
getActiveIdentityProviders,
getLoginSettings,
getOrgsByDomainSuffix,
listAuthenticationMethodTypes,
listUsers,
startIdentityProviderFlow,
@@ -9,6 +10,8 @@ import {
import { createSessionForUserIdAndUpdateCookie } from "@/utils/session";
import { NextRequest, NextResponse } from "next/server";
const ORG_SUFFIX_REGEX = /(?<=@)(.+)/;
export async function POST(request: NextRequest) {
const body = await request.json();
if (body) {
@@ -104,14 +107,37 @@ export async function POST(request: NextRequest) {
loginSettings?.allowRegister &&
loginSettings?.allowUsernamePassword
) {
const params: any = { organization };
let orgToRegisterOn: string | undefined = organization;
if (
!orgToRegisterOn &&
loginName &&
ORG_SUFFIX_REGEX.test(loginName)
) {
const matched = ORG_SUFFIX_REGEX.exec(loginName);
const suffix = matched?.[1] ?? "";
const orgs = await getOrgsByDomainSuffix(suffix);
orgToRegisterOn =
orgs.result && orgs.result.length === 1
? orgs.result[0].id
: undefined;
}
const params: any = {};
if (authRequestId) {
params.authRequestId = authRequestId;
}
if (loginName) {
params.email = loginName;
}
if (orgToRegisterOn) {
params.organization = orgToRegisterOn;
}
const registerUrl = new URL(
"/register?" + new URLSearchParams(params),
request.url,

View File

@@ -5,6 +5,7 @@ import {
createUserServiceClient,
createIdpServiceClient,
makeReqCtx,
createOrganizationServiceClient,
} from "@zitadel/client/v2";
import { createManagementServiceClient } from "@zitadel/client/v1";
import { createServerTransport } from "@zitadel/node";
@@ -36,10 +37,10 @@ const transport = createServerTransport(
);
export const sessionService = createSessionServiceClient(transport);
export const managementService = createManagementServiceClient(transport);
export const userService = createUserServiceClient(transport);
export const oidcService = createOIDCServiceClient(transport);
export const idpService = createIdpServiceClient(transport);
export const orgService = createOrganizationServiceClient(transport);
export const settingsService = createSettingsServiceClient(transport);
@@ -292,8 +293,11 @@ export async function listUsers({
);
}
export async function getOrgByDomain(domain: string) {
return managementService.getOrgByDomainGlobal({ domain }, {});
export async function getOrgsByDomainSuffix(domain: string) {
return orgService.listOrganizations(
{ queries: [{ query: { case: "domainQuery", value: { domain } } }] },
{},
);
}
export async function startIdentityProviderFlow({