redirect from server

This commit is contained in:
Max Peintner
2024-10-28 16:02:58 +01:00
parent b8039be958
commit 357d462b16
2 changed files with 20 additions and 19 deletions

View File

@@ -1,6 +1,6 @@
"use client";
import { registerUser, RegisterUserResponse } from "@/lib/server/register";
import { registerUser } from "@/lib/server/register";
import { LegalAndSupportSettings } from "@zitadel/proto/zitadel/settings/v2/legal_settings_pb";
import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation";
@@ -66,6 +66,7 @@ export function RegisterFormWithoutPassword({
firstName: values.firstname,
lastName: values.lastname,
organization: organization,
authRequestId: authRequestId,
}).catch((error) => {
setError("Could not register user");
setLoading(false);
@@ -98,22 +99,7 @@ export function RegisterFormWithoutPassword({
if (withPassword) {
return router.push(`/register?` + new URLSearchParams(registerParams));
} else {
const session = (await submitAndRegister(value)) as RegisterUserResponse;
const params = new URLSearchParams({});
if (session?.factors?.user?.loginName) {
params.set("loginName", session.factors?.user?.loginName);
}
if (organization) {
params.set("organization", organization);
}
if (authRequestId) {
params.set("authRequestId", authRequestId);
}
return router.push(`/passkey/set?` + new URLSearchParams(params));
return submitAndRegister(value);
}
}

View File

@@ -8,6 +8,7 @@ import {
ChecksJson,
ChecksSchema,
} from "@zitadel/proto/zitadel/session/v2/session_service_pb";
import { redirect } from "next/navigation";
type RegisterUserCommand = {
email: string;
@@ -23,7 +24,6 @@ export type RegisterUserResponse = {
sessionId: string;
factors: Factors | undefined;
};
export async function registerUser(command: RegisterUserCommand) {
const human = await addHumanUser({
email: command.email,
@@ -50,7 +50,7 @@ export async function registerUser(command: RegisterUserCommand) {
const checks = create(ChecksSchema, checkPayload);
return createSessionAndUpdateCookie(
const session = await createSessionAndUpdateCookie(
checks,
undefined,
command.authRequestId,
@@ -61,4 +61,19 @@ export async function registerUser(command: RegisterUserCommand) {
factors: session.factors,
};
});
if (!session || !session.factors?.user) {
return { error: "Could not create session" };
}
const params = new URLSearchParams({
loginName: session.factors.user.loginName,
organization: session.factors.user.organizationId,
});
if (command.authRequestId) {
params.append("authRequestId", command.authRequestId);
}
return redirect("/passkey/set?" + params);
}