2023-07-18 13:58:33 +02:00
|
|
|
import { ReactNode } from "react";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
ZitadelServer,
|
|
|
|
|
settings,
|
|
|
|
|
GetActiveIdentityProvidersResponse,
|
|
|
|
|
IdentityProvider,
|
|
|
|
|
IdentityProviderType,
|
|
|
|
|
} from "@zitadel/server";
|
2023-07-27 11:05:42 +02:00
|
|
|
import {
|
|
|
|
|
SignInWithGitlab,
|
|
|
|
|
SignInWithAzureAD,
|
|
|
|
|
SignInWithGoogle,
|
|
|
|
|
SignInWithGithub,
|
|
|
|
|
} from "@zitadel/react";
|
|
|
|
|
import { server, startIdentityProviderFlow } from "#/lib/zitadel";
|
2023-07-18 13:58:33 +02:00
|
|
|
|
|
|
|
|
export interface SignInWithIDPProps {
|
|
|
|
|
children?: ReactNode;
|
|
|
|
|
server: ZitadelServer;
|
|
|
|
|
orgId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getIdentityProviders(
|
|
|
|
|
server: ZitadelServer,
|
|
|
|
|
orgId?: string
|
|
|
|
|
): Promise<IdentityProvider[] | undefined> {
|
|
|
|
|
const settingsService = settings.getSettings(server);
|
|
|
|
|
console.log("req");
|
|
|
|
|
return settingsService
|
|
|
|
|
.getActiveIdentityProviders(
|
|
|
|
|
orgId ? { ctx: { orgId } } : { ctx: { instance: true } },
|
|
|
|
|
{}
|
|
|
|
|
)
|
|
|
|
|
.then((resp: GetActiveIdentityProvidersResponse) => {
|
|
|
|
|
return resp.identityProviders;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 11:05:42 +02:00
|
|
|
export async function SignInWithIDP(props: SignInWithIDPProps) {
|
2023-07-26 10:41:12 +02:00
|
|
|
console.log(props.server);
|
2023-07-27 11:05:42 +02:00
|
|
|
const identityProviders = await getIdentityProviders(
|
|
|
|
|
props.server,
|
|
|
|
|
props.orgId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(identityProviders);
|
2023-07-18 13:58:33 +02:00
|
|
|
|
2023-07-27 11:05:42 +02:00
|
|
|
function startFlow(idp: IdentityProvider) {
|
|
|
|
|
return startIdentityProviderFlow(server, {
|
|
|
|
|
idpId: idp.id,
|
|
|
|
|
successUrl: "",
|
|
|
|
|
failureUrl: "",
|
|
|
|
|
}).then(() => {});
|
|
|
|
|
}
|
2023-07-18 13:58:33 +02:00
|
|
|
|
|
|
|
|
return (
|
2023-07-27 11:05:42 +02:00
|
|
|
<div className="flex flex-col w-full space-y-2 text-sm">
|
|
|
|
|
{identityProviders &&
|
2023-07-18 13:58:33 +02:00
|
|
|
identityProviders.map((idp, i) => {
|
|
|
|
|
switch (idp.type) {
|
|
|
|
|
case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB:
|
2023-07-26 09:30:39 +02:00
|
|
|
return (
|
|
|
|
|
<SignInWithGithub
|
|
|
|
|
key={`idp-${i}`}
|
|
|
|
|
name={idp.name}
|
|
|
|
|
></SignInWithGithub>
|
|
|
|
|
);
|
2023-07-18 13:58:33 +02:00
|
|
|
case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB_ES:
|
2023-07-26 09:30:39 +02:00
|
|
|
return (
|
|
|
|
|
<SignInWithGithub
|
|
|
|
|
key={`idp-${i}`}
|
|
|
|
|
name={idp.name}
|
|
|
|
|
></SignInWithGithub>
|
|
|
|
|
);
|
2023-07-18 13:58:33 +02:00
|
|
|
case IdentityProviderType.IDENTITY_PROVIDER_TYPE_AZURE_AD:
|
2023-07-26 09:30:39 +02:00
|
|
|
return (
|
|
|
|
|
<SignInWithAzureAD
|
|
|
|
|
key={`idp-${i}`}
|
|
|
|
|
name={idp.name}
|
|
|
|
|
></SignInWithAzureAD>
|
|
|
|
|
);
|
2023-07-18 13:58:33 +02:00
|
|
|
case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GOOGLE:
|
2023-07-26 09:30:39 +02:00
|
|
|
return (
|
|
|
|
|
<SignInWithGoogle
|
|
|
|
|
key={`idp-${i}`}
|
|
|
|
|
name={idp.name}
|
2023-07-27 11:05:42 +02:00
|
|
|
onClick={() => startFlow(idp)}
|
2023-07-26 09:30:39 +02:00
|
|
|
></SignInWithGoogle>
|
|
|
|
|
);
|
2023-07-18 13:58:33 +02:00
|
|
|
case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB:
|
2023-07-26 09:30:39 +02:00
|
|
|
return (
|
|
|
|
|
<SignInWithGitlab
|
|
|
|
|
key={`idp-${i}`}
|
|
|
|
|
name={idp.name}
|
|
|
|
|
></SignInWithGitlab>
|
|
|
|
|
);
|
2023-07-18 13:58:33 +02:00
|
|
|
case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB_SELF_HOSTED:
|
2023-07-26 09:30:39 +02:00
|
|
|
return (
|
|
|
|
|
<SignInWithGitlab
|
|
|
|
|
key={`idp-${i}`}
|
|
|
|
|
name={idp.name}
|
|
|
|
|
></SignInWithGitlab>
|
|
|
|
|
);
|
2023-07-18 13:58:33 +02:00
|
|
|
default:
|
2023-07-26 09:30:39 +02:00
|
|
|
return <div>{idp.name}</div>;
|
2023-07-18 13:58:33 +02:00
|
|
|
}
|
2023-07-27 11:05:42 +02:00
|
|
|
})}
|
2023-07-18 13:58:33 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SignInWithIDP.displayName = "SignInWithIDP";
|