mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 05:12:20 +00:00
find org context from loginname
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user