2024-08-29 16:25:44 +02:00
|
|
|
"use server";
|
|
|
|
|
|
|
|
|
|
import { headers } from "next/headers";
|
2024-09-05 13:48:33 +02:00
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
|
import { createSessionForUserIdAndUpdateCookie } from "../../utils/session";
|
2024-08-29 16:25:44 +02:00
|
|
|
import { idpTypeToSlug } from "../idp";
|
|
|
|
|
import {
|
|
|
|
|
getActiveIdentityProviders,
|
|
|
|
|
getLoginSettings,
|
|
|
|
|
listAuthenticationMethodTypes,
|
|
|
|
|
listUsers,
|
|
|
|
|
startIdentityProviderFlow,
|
|
|
|
|
} from "../zitadel";
|
|
|
|
|
|
2024-08-30 09:52:42 +02:00
|
|
|
export type SendLoginnameCommand = {
|
2024-08-29 16:25:44 +02:00
|
|
|
loginName: string;
|
|
|
|
|
authRequestId?: string;
|
|
|
|
|
organization?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-05 13:38:03 +02:00
|
|
|
export async function sendLoginname(command: SendLoginnameCommand) {
|
2024-08-29 16:25:44 +02:00
|
|
|
const users = await listUsers({
|
2024-09-05 13:38:03 +02:00
|
|
|
userName: command.loginName,
|
|
|
|
|
organizationId: command.organization,
|
2024-08-29 16:25:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (users.details?.totalResult == BigInt(1) && users.result[0].userId) {
|
|
|
|
|
const userId = users.result[0].userId;
|
|
|
|
|
const session = await createSessionForUserIdAndUpdateCookie(
|
|
|
|
|
userId,
|
|
|
|
|
undefined,
|
|
|
|
|
undefined,
|
2024-09-05 13:38:03 +02:00
|
|
|
command.authRequestId,
|
2024-08-29 16:25:44 +02:00
|
|
|
);
|
|
|
|
|
|
2024-08-30 09:52:42 +02:00
|
|
|
if (!session.factors?.user?.id) {
|
|
|
|
|
throw Error("Could not create session for user");
|
2024-08-29 16:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const methods = await listAuthenticationMethodTypes(
|
|
|
|
|
session.factors?.user?.id,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
authMethodTypes: methods.authMethodTypes,
|
|
|
|
|
sessionId: session.id,
|
|
|
|
|
factors: session.factors,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 13:38:03 +02:00
|
|
|
const loginSettings = await getLoginSettings(command.organization);
|
2024-08-29 16:25:44 +02:00
|
|
|
// TODO: check if allowDomainDiscovery has to be allowed too, to redirect to the register page
|
|
|
|
|
// user not found, check if register is enabled on organization
|
|
|
|
|
|
|
|
|
|
if (loginSettings?.allowRegister && !loginSettings?.allowUsernamePassword) {
|
|
|
|
|
// TODO redirect to loginname page with idp hint
|
|
|
|
|
const identityProviders = await getActiveIdentityProviders(
|
2024-09-05 13:38:03 +02:00
|
|
|
command.organization,
|
2024-08-29 16:25:44 +02:00
|
|
|
).then((resp) => {
|
|
|
|
|
return resp.identityProviders;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (identityProviders.length === 1) {
|
|
|
|
|
const host = headers().get("host");
|
|
|
|
|
console.log("host", host);
|
|
|
|
|
const identityProviderType = identityProviders[0].type;
|
|
|
|
|
|
|
|
|
|
const provider = idpTypeToSlug(identityProviderType);
|
|
|
|
|
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
2024-09-05 13:38:03 +02:00
|
|
|
if (command.authRequestId) {
|
|
|
|
|
params.set("authRequestId", command.authRequestId);
|
2024-08-29 16:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 13:38:03 +02:00
|
|
|
if (command.organization) {
|
|
|
|
|
params.set("organization", command.organization);
|
2024-08-29 16:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return startIdentityProviderFlow({
|
|
|
|
|
idpId: identityProviders[0].id,
|
|
|
|
|
urls: {
|
|
|
|
|
successUrl:
|
|
|
|
|
`${host}/idp/${provider}/success?` + new URLSearchParams(params),
|
|
|
|
|
failureUrl:
|
|
|
|
|
`${host}/idp/${provider}/failure?` + new URLSearchParams(params),
|
|
|
|
|
},
|
|
|
|
|
}).then((resp: any) => {
|
|
|
|
|
if (resp.authUrl) {
|
|
|
|
|
return redirect(resp.authUrl);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2024-09-03 10:24:05 +02:00
|
|
|
throw Error("Could not find user");
|
2024-08-29 16:25:44 +02:00
|
|
|
}
|
|
|
|
|
} else if (
|
|
|
|
|
loginSettings?.allowRegister &&
|
|
|
|
|
loginSettings?.allowUsernamePassword
|
|
|
|
|
) {
|
2024-09-05 13:38:03 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
|
|
|
|
if (command.organization) {
|
|
|
|
|
params.set("organization", command.organization);
|
|
|
|
|
}
|
|
|
|
|
if (command.authRequestId) {
|
|
|
|
|
params.set("authRequestId", command.authRequestId);
|
2024-08-29 16:25:44 +02:00
|
|
|
}
|
2024-09-05 13:38:03 +02:00
|
|
|
if (command.loginName) {
|
|
|
|
|
params.set("loginName", command.loginName);
|
2024-08-29 16:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 13:38:03 +02:00
|
|
|
const registerUrl = new URL("/register?" + params);
|
2024-08-29 16:25:44 +02:00
|
|
|
|
|
|
|
|
return redirect(registerUrl.toString());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 10:24:05 +02:00
|
|
|
throw Error("Could not find user");
|
2024-08-29 16:25:44 +02:00
|
|
|
}
|