mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 06:52:24 +00:00
find org context from loginname
This commit is contained in:
@@ -2,6 +2,7 @@ import { idpTypeToSlug } from "@/lib/idp";
|
|||||||
import {
|
import {
|
||||||
getActiveIdentityProviders,
|
getActiveIdentityProviders,
|
||||||
getLoginSettings,
|
getLoginSettings,
|
||||||
|
getOrgsByDomainSuffix,
|
||||||
listAuthenticationMethodTypes,
|
listAuthenticationMethodTypes,
|
||||||
listUsers,
|
listUsers,
|
||||||
startIdentityProviderFlow,
|
startIdentityProviderFlow,
|
||||||
@@ -9,6 +10,8 @@ import {
|
|||||||
import { createSessionForUserIdAndUpdateCookie } from "@/utils/session";
|
import { createSessionForUserIdAndUpdateCookie } from "@/utils/session";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
const ORG_SUFFIX_REGEX = /(?<=@)(.+)/;
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
if (body) {
|
if (body) {
|
||||||
@@ -104,14 +107,37 @@ export async function POST(request: NextRequest) {
|
|||||||
loginSettings?.allowRegister &&
|
loginSettings?.allowRegister &&
|
||||||
loginSettings?.allowUsernamePassword
|
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) {
|
if (authRequestId) {
|
||||||
params.authRequestId = authRequestId;
|
params.authRequestId = authRequestId;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loginName) {
|
if (loginName) {
|
||||||
params.email = loginName;
|
params.email = loginName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (orgToRegisterOn) {
|
||||||
|
params.organization = orgToRegisterOn;
|
||||||
|
}
|
||||||
|
|
||||||
const registerUrl = new URL(
|
const registerUrl = new URL(
|
||||||
"/register?" + new URLSearchParams(params),
|
"/register?" + new URLSearchParams(params),
|
||||||
request.url,
|
request.url,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
createUserServiceClient,
|
createUserServiceClient,
|
||||||
createIdpServiceClient,
|
createIdpServiceClient,
|
||||||
makeReqCtx,
|
makeReqCtx,
|
||||||
|
createOrganizationServiceClient,
|
||||||
} from "@zitadel/client/v2";
|
} from "@zitadel/client/v2";
|
||||||
import { createManagementServiceClient } from "@zitadel/client/v1";
|
import { createManagementServiceClient } from "@zitadel/client/v1";
|
||||||
import { createServerTransport } from "@zitadel/node";
|
import { createServerTransport } from "@zitadel/node";
|
||||||
@@ -36,10 +37,10 @@ const transport = createServerTransport(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const sessionService = createSessionServiceClient(transport);
|
export const sessionService = createSessionServiceClient(transport);
|
||||||
export const managementService = createManagementServiceClient(transport);
|
|
||||||
export const userService = createUserServiceClient(transport);
|
export const userService = createUserServiceClient(transport);
|
||||||
export const oidcService = createOIDCServiceClient(transport);
|
export const oidcService = createOIDCServiceClient(transport);
|
||||||
export const idpService = createIdpServiceClient(transport);
|
export const idpService = createIdpServiceClient(transport);
|
||||||
|
export const orgService = createOrganizationServiceClient(transport);
|
||||||
|
|
||||||
export const settingsService = createSettingsServiceClient(transport);
|
export const settingsService = createSettingsServiceClient(transport);
|
||||||
|
|
||||||
@@ -292,8 +293,11 @@ export async function listUsers({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getOrgByDomain(domain: string) {
|
export async function getOrgsByDomainSuffix(domain: string) {
|
||||||
return managementService.getOrgByDomainGlobal({ domain }, {});
|
return orgService.listOrganizations(
|
||||||
|
{ queries: [{ query: { case: "domainQuery", value: { domain } } }] },
|
||||||
|
{},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function startIdentityProviderFlow({
|
export async function startIdentityProviderFlow({
|
||||||
|
|||||||
Reference in New Issue
Block a user