catch org context

This commit is contained in:
peintnermax
2024-03-11 17:33:19 +01:00
parent 49b86d2df1
commit 7668113b96
3 changed files with 37 additions and 38 deletions

View File

@@ -1,22 +0,0 @@
import { headers } from "next/headers";
export default function Page() {
const headersList = headers();
const hds = [
"x-zitadel-login-client",
"forwarded",
"x-zitadel-forwarded",
"host",
"referer",
];
return (
<div className="space-y-8">
<h1 className="text-xl font-medium">Headers</h1>
{hds.map((h) => (
<p key={h}>
{h}:{headersList.get(h)}
</p>
))}
</div>
);
}

View File

@@ -3,11 +3,14 @@ import React from "react";
import { getBrandingSettings, server } from "#/lib/zitadel";
import { Logo } from "#/ui/Logo";
export default async function Template({
export default async function Layout({
children,
params,
}: {
children: React.ReactNode;
params: any;
}) {
console.log(params);
const branding = await getBrandingSettings(server);
let partial: Partial<BrandingSettings> | undefined;
if (branding) {

View File

@@ -5,7 +5,7 @@ import {
server,
} from "#/lib/zitadel";
import { SessionCookie, getAllSessions } from "#/utils/cookies";
import { Session, AuthRequest, Prompt } from "@zitadel/server";
import { Session, AuthRequest, Prompt, login } from "@zitadel/server";
import { NextRequest, NextResponse } from "next/server";
async function loadSessions(ids: string[]): Promise<Session[]> {
@@ -82,25 +82,29 @@ export async function GET(request: NextRequest) {
if (authRequestId) {
console.log(`Login with authRequest: ${authRequestId}`);
const { authRequest } = await getAuthRequest(server, { authRequestId });
let organization;
if (
authRequest?.scope &&
authRequest.scope.find((s) => ORG_SCOPE_REGEX.test(s))
) {
const orgId = authRequest.scope.find((s) => ORG_SCOPE_REGEX.test(s));
if (orgId) {
const matched = orgId.replace("urn:zitadel:iam:org:id:", "");
organization = matched;
}
}
if (authRequest && authRequest.prompt.includes(Prompt.PROMPT_CREATE)) {
const registerUrl = new URL("/register", request.url);
if (authRequest?.id) {
registerUrl.searchParams.set("authRequestId", authRequest?.id);
}
if (
authRequest.scope &&
authRequest.scope.find((s) => ORG_SCOPE_REGEX.test(s))
) {
const orgId = authRequest.scope
.find((s) => ORG_SCOPE_REGEX.test(s))
?.match(ORG_SCOPE_REGEX)?.[1];
console.log(orgId);
if (orgId) {
registerUrl.searchParams.set("orgId", orgId);
}
if (organization) {
registerUrl.searchParams.set("organization", organization);
}
return NextResponse.redirect(registerUrl);
}
@@ -112,6 +116,9 @@ export async function GET(request: NextRequest) {
if (authRequest?.id) {
accountsUrl.searchParams.set("authRequestId", authRequest?.id);
}
if (organization) {
accountsUrl.searchParams.set("organization", organization);
}
return NextResponse.redirect(accountsUrl);
} else if (authRequest.prompt.includes(Prompt.PROMPT_LOGIN)) {
@@ -123,6 +130,9 @@ export async function GET(request: NextRequest) {
if (authRequest.loginHint) {
loginNameUrl.searchParams.set("loginName", authRequest.loginHint);
}
if (organization) {
loginNameUrl.searchParams.set("organization", organization);
}
return NextResponse.redirect(loginNameUrl);
} else if (authRequest.prompt.includes(Prompt.PROMPT_NONE)) {
// NONE prompt - silent authentication
@@ -178,13 +188,17 @@ export async function GET(request: NextRequest) {
} else {
const accountsUrl = new URL("/accounts", request.url);
accountsUrl.searchParams.set("authRequestId", authRequestId);
if (organization) {
accountsUrl.searchParams.set("organization", organization);
}
return NextResponse.redirect(accountsUrl);
}
} else {
const accountsUrl = new URL("/accounts", request.url);
accountsUrl.searchParams.set("authRequestId", authRequestId);
if (organization) {
accountsUrl.searchParams.set("organization", organization);
}
return NextResponse.redirect(accountsUrl);
}
}
@@ -197,6 +211,10 @@ export async function GET(request: NextRequest) {
loginNameUrl.searchParams.set("submit", "true"); // autosubmit
}
if (organization) {
loginNameUrl.searchParams.set("organization", organization);
}
return NextResponse.redirect(loginNameUrl);
}
} else {