"use client"; import { ReactNode, useState } from "react"; import { SignInWithGitlab, SignInWithAzureAD, SignInWithGoogle, SignInWithGithub, } from "@zitadel/react"; import { useRouter } from "next/navigation"; import { ProviderSlug } from "#/lib/demos"; import Alert from "./Alert"; export interface SignInWithIDPProps { children?: ReactNode; host: string; identityProviders: any[]; startIDPFlowPath?: (idpId: string) => string; } const START_IDP_FLOW_PATH = (idpId: string) => `/v2beta/users/idps/${idpId}/start`; export function SignInWithIDP({ host, identityProviders, startIDPFlowPath = START_IDP_FLOW_PATH, }: SignInWithIDPProps) { const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const router = useRouter(); async function startFlow(idpId: string, provider: ProviderSlug) { setLoading(true); const res = await fetch("/api/idp/start", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ idpId, successUrl: `${host}/register/idp/${provider}/success`, failureUrl: `${host}/register/idp/${provider}/failure`, }), }); const response = await res.json(); setLoading(false); if (!res.ok) { setError(response.details); return Promise.reject(response.details); } return response; } return (
{identityProviders && identityProviders.map((idp, i) => { switch (idp.type) { case 6: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB: return ( startFlow(idp.id, ProviderSlug.GITHUB).then( ({ authUrl }) => { router.push(authUrl); } ) } > ); case 7: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB_ES: return ( alert("TODO: unimplemented")} > ); case 5: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_AZURE_AD: return ( alert("TODO: unimplemented")} > ); case 10: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GOOGLE: return ( startFlow(idp.id, ProviderSlug.GOOGLE).then( ({ authUrl }) => { router.push(authUrl); } ) } > ); case 8: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB: return ( alert("TODO: unimplemented")} > ); case 9: //IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB_SELF_HOSTED: return ( alert("TODO: unimplemented")} > ); default: return null; } })} {error && (
{error}
)}
); } SignInWithIDP.displayName = "SignInWithIDP";