Files
zitadel/apps/login/ui/SignInWithIDP.tsx

114 lines
3.3 KiB
TypeScript
Raw Normal View History

"use client";
import { ReactNode, useState } from "react";
// import { IdentityProviderType } from "@zitadel/server";
// import { IdentityProvider } from "@zitadel/client";
2023-07-18 13:58:33 +02:00
2023-07-27 11:05:42 +02:00
import {
SignInWithGitlab,
SignInWithAzureAD,
SignInWithGoogle,
SignInWithGithub,
} from "@zitadel/react";
import { useRouter } from "next/navigation";
2023-07-18 13:58:33 +02:00
export interface SignInWithIDPProps {
children?: ReactNode;
identityProviders: any[];
2023-07-18 13:58:33 +02:00
}
export function SignInWithIDP({ identityProviders }: SignInWithIDPProps) {
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string>("");
const router = useRouter();
2023-07-18 13:58:33 +02:00
async function startFlow(idp: any) {
console.log("start flow");
const host = "http://localhost:3000";
setLoading(true);
const res = await fetch("/api/idp/start", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
idpId: idp.id,
successUrl: `${host}/api/idp/success`,
failureUrl: `${host}/api/idp/success`,
}),
});
2023-07-27 11:05:42 +02:00
const response = await res.json();
2023-07-18 13:58:33 +02:00
setLoading(false);
if (!res.ok) {
setError(response.details);
return Promise.reject(response.details);
}
return response;
2023-07-27 11:05:42 +02:00
}
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 6: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB:
2023-07-26 09:30:39 +02:00
return (
<SignInWithGithub
key={`idp-${i}`}
name={idp.name}
></SignInWithGithub>
);
case 7: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB_ES:
2023-07-26 09:30:39 +02:00
return (
<SignInWithGithub
key={`idp-${i}`}
name={idp.name}
></SignInWithGithub>
);
case 5: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_AZURE_AD:
2023-07-26 09:30:39 +02:00
return (
<SignInWithAzureAD
key={`idp-${i}`}
name={idp.name}
></SignInWithAzureAD>
);
case 10: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GOOGLE:
2023-07-26 09:30:39 +02:00
return (
<SignInWithGoogle
key={`idp-${i}`}
name={idp.name}
onClick={() =>
startFlow(idp).then(({ authUrl }) => {
console.log("done");
router.push(authUrl);
})
}
2023-07-26 09:30:39 +02:00
></SignInWithGoogle>
);
case 8: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB:
2023-07-26 09:30:39 +02:00
return (
<SignInWithGitlab
key={`idp-${i}`}
name={idp.name}
></SignInWithGitlab>
);
case 9: //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";