From f69d922bc1fad4d412221dbacc0cd49b78784b4c Mon Sep 17 00:00:00 2001 From: peintnermax Date: Thu, 27 Jul 2023 15:54:34 +0200 Subject: [PATCH] fix async await client, react components --- apps/login/app/(login)/register/idp/page.tsx | 30 ++++- apps/login/app/api/idp/start/route.ts | 19 ++++ apps/login/package.json | 3 +- apps/login/ui/SignInWithIDP.tsx | 92 +++++++-------- packages/zitadel-client/package.json | 6 +- packages/zitadel-client/src/index.ts | 8 ++ packages/zitadel-client/tsup.config.ts | 13 +++ packages/zitadel-react/package.json | 4 +- .../src/components/SignInWith.tsx | 10 +- .../src/components/SignInWithAzureAD.tsx | 37 +++--- .../src/components/SignInWithGithub.tsx | 35 +++--- .../src/components/SignInWithGitlab.tsx | 33 +++--- .../src/components/SignInWithGoogle.tsx | 18 +-- packages/zitadel-react/src/index.tsx | 15 +-- pnpm-lock.yaml | 105 ++++++++++-------- 15 files changed, 257 insertions(+), 171 deletions(-) create mode 100644 apps/login/app/api/idp/start/route.ts create mode 100644 packages/zitadel-client/tsup.config.ts diff --git a/apps/login/app/(login)/register/idp/page.tsx b/apps/login/app/(login)/register/idp/page.tsx index 7313e33eced..e309f38b5e1 100644 --- a/apps/login/app/(login)/register/idp/page.tsx +++ b/apps/login/app/(login)/register/idp/page.tsx @@ -1,5 +1,27 @@ import { getLegalAndSupportSettings, server } from "#/lib/zitadel"; import { SignInWithIDP } from "#/ui/SignInWithIDP"; +import { + GetActiveIdentityProvidersResponse, + IdentityProvider, + ZitadelServer, + settings, +} from "@zitadel/server"; + +function getIdentityProviders( + server: ZitadelServer, + orgId?: string +): Promise { + const settingsService = settings.getSettings(server); + console.log("req"); + return settingsService + .getActiveIdentityProviders( + orgId ? { ctx: { orgId } } : { ctx: { instance: true } }, + {} + ) + .then((resp: GetActiveIdentityProvidersResponse) => { + return resp.identityProviders; + }); +} export default async function Page({ searchParams, @@ -8,14 +30,18 @@ export default async function Page({ }) { const legal = await getLegalAndSupportSettings(server); - console.log(server); + const identityProviders = await getIdentityProviders(server, ""); + + console.log(identityProviders); return (

Register

Create your ZITADEL account.

- {legal && } + {legal && identityProviders && ( + + )}
); } diff --git a/apps/login/app/api/idp/start/route.ts b/apps/login/app/api/idp/start/route.ts new file mode 100644 index 00000000000..84ef0547a25 --- /dev/null +++ b/apps/login/app/api/idp/start/route.ts @@ -0,0 +1,19 @@ +import { server, startIdentityProviderFlow } from "#/lib/zitadel"; +import { NextRequest, NextResponse } from "next/server"; + +export async function POST(request: NextRequest) { + const body = await request.json(); + if (body) { + let { idpId, successUrl, failureUrl } = body; + + return startIdentityProviderFlow(server, { idpId, successUrl, failureUrl }) + .then((resp) => { + return NextResponse.json(resp); + }) + .catch((error) => { + return NextResponse.json(error, { status: 500 }); + }); + } else { + return NextResponse.json({}, { status: 400 }); + } +} diff --git a/apps/login/package.json b/apps/login/package.json index cd355da39db..73dd6860921 100644 --- a/apps/login/package.json +++ b/apps/login/package.json @@ -36,13 +36,14 @@ "@heroicons/react": "2.0.13", "@tailwindcss/forms": "0.5.3", "@vercel/analytics": "^1.0.0", + "@zitadel/client": "workspace:*", "@zitadel/next": "workspace:*", "@zitadel/react": "workspace:*", "@zitadel/server": "workspace:*", "clsx": "1.2.1", "date-fns": "2.29.3", "moment": "^2.29.4", - "next": "13.4.7", + "next": "13.4.12", "next-themes": "^0.2.1", "nice-grpc": "2.0.1", "react": "18.2.0", diff --git a/apps/login/ui/SignInWithIDP.tsx b/apps/login/ui/SignInWithIDP.tsx index cca46862456..11f8c36f330 100644 --- a/apps/login/ui/SignInWithIDP.tsx +++ b/apps/login/ui/SignInWithIDP.tsx @@ -1,57 +1,52 @@ -import { ReactNode } from "react"; +"use client"; +import { ReactNode, useState } from "react"; + +// import { IdentityProviderType } from "@zitadel/server"; +// import { IdentityProvider } from "@zitadel/client"; -import { - ZitadelServer, - settings, - GetActiveIdentityProvidersResponse, - IdentityProvider, - IdentityProviderType, -} from "@zitadel/server"; import { SignInWithGitlab, SignInWithAzureAD, SignInWithGoogle, SignInWithGithub, } from "@zitadel/react"; -import { server, startIdentityProviderFlow } from "#/lib/zitadel"; +import { useRouter } from "next/navigation"; export interface SignInWithIDPProps { children?: ReactNode; - server: ZitadelServer; - orgId?: string; + identityProviders: any[]; } -function getIdentityProviders( - server: ZitadelServer, - orgId?: string -): Promise { - const settingsService = settings.getSettings(server); - console.log("req"); - return settingsService - .getActiveIdentityProviders( - orgId ? { ctx: { orgId } } : { ctx: { instance: true } }, - {} - ) - .then((resp: GetActiveIdentityProvidersResponse) => { - return resp.identityProviders; +export function SignInWithIDP({ identityProviders }: SignInWithIDPProps) { + const [loading, setLoading] = useState(false); + const [error, setError] = useState(""); + const router = useRouter(); + + 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`, + }), }); -} -export async function SignInWithIDP(props: SignInWithIDPProps) { - console.log(props.server); - const identityProviders = await getIdentityProviders( - props.server, - props.orgId - ); + const response = await res.json(); - console.log(identityProviders); - - function startFlow(idp: IdentityProvider) { - return startIdentityProviderFlow(server, { - idpId: idp.id, - successUrl: "", - failureUrl: "", - }).then(() => {}); + setLoading(false); + if (!res.ok) { + setError(response.details); + return Promise.reject(response.details); + } + return response; } return ( @@ -59,43 +54,48 @@ export async function SignInWithIDP(props: SignInWithIDPProps) { {identityProviders && identityProviders.map((idp, i) => { switch (idp.type) { - case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB: + case 6: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB: return ( ); - case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB_ES: + case 7: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITHUB_ES: return ( ); - case IdentityProviderType.IDENTITY_PROVIDER_TYPE_AZURE_AD: + case 5: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_AZURE_AD: return ( ); - case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GOOGLE: + case 10: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GOOGLE: return ( startFlow(idp)} + onClick={() => + startFlow(idp).then(({ authUrl }) => { + console.log("done"); + router.push(authUrl); + }) + } > ); - case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB: + case 8: // IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB: return ( ); - case IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB_SELF_HOSTED: + case 9: //IdentityProviderType.IDENTITY_PROVIDER_TYPE_GITLAB_SELF_HOSTED: return ( ({ + treeshake: true, + splitting: true, + publicDir: true, + entry: ["src/index.ts", "src/**/index.ts"], + format: ["esm", "cjs"], + dts: true, + minify: true, + clean: true, + ...options, +})); diff --git a/packages/zitadel-react/package.json b/packages/zitadel-react/package.json index 18f5df75cb7..e2abee12bdd 100644 --- a/packages/zitadel-react/package.json +++ b/packages/zitadel-react/package.json @@ -26,8 +26,8 @@ "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^14.0.0", "@types/jest": "^29.5.1", - "@types/react": "^17.0.13", - "@types/react-dom": "^17.0.8", + "@types/react": "^18.2.17", + "@types/react-dom": "^18.2.7", "@types/testing-library__jest-dom": "^5.14.6", "@zitadel/tsconfig": "workspace:*", "autoprefixer": "10.4.13", diff --git a/packages/zitadel-react/src/components/SignInWith.tsx b/packages/zitadel-react/src/components/SignInWith.tsx index e35fd55208e..f99938a1a25 100644 --- a/packages/zitadel-react/src/components/SignInWith.tsx +++ b/packages/zitadel-react/src/components/SignInWith.tsx @@ -1,4 +1,8 @@ -export interface SignInWithIdentityProviderProps { - children?: React.ReactNode; +import { ButtonHTMLAttributes, DetailedHTMLProps } from "react"; + +export type SignInWithIdentityProviderProps = DetailedHTMLProps< + ButtonHTMLAttributes, + HTMLButtonElement +> & { name?: string; -} +}; diff --git a/packages/zitadel-react/src/components/SignInWithAzureAD.tsx b/packages/zitadel-react/src/components/SignInWithAzureAD.tsx index 23f03546bef..4a2161ac5d2 100644 --- a/packages/zitadel-react/src/components/SignInWithAzureAD.tsx +++ b/packages/zitadel-react/src/components/SignInWithAzureAD.tsx @@ -1,18 +1,25 @@ +"use client"; + +import { ReactNode, forwardRef } from "react"; import { SignInWithIdentityProviderProps } from "./SignInWith"; -export interface SignInWithAzureADProps - extends SignInWithIdentityProviderProps {} - -export function SignInWithAzureAD(props: SignInWithAzureADProps) { - return ( -
+export const SignInWithAzureAD = forwardRef< + HTMLButtonElement, + SignInWithIdentityProviderProps +>( + ({ children, className = "", name = "", ...props }, ref): ReactNode => ( +
- ); -} + {children ? ( + children + ) : ( + + {name ? name : "Sign in with AzureAD"} + + )} + + ) +); SignInWithAzureAD.displayName = "SignInWithAzureAD"; diff --git a/packages/zitadel-react/src/components/SignInWithGithub.tsx b/packages/zitadel-react/src/components/SignInWithGithub.tsx index b898240c0d1..e6c1c181270 100644 --- a/packages/zitadel-react/src/components/SignInWithGithub.tsx +++ b/packages/zitadel-react/src/components/SignInWithGithub.tsx @@ -1,18 +1,25 @@ +"use client"; + +import { ReactNode, forwardRef } from "react"; import { SignInWithIdentityProviderProps } from "./SignInWith"; -export interface SignInWithGithubProps - extends SignInWithIdentityProviderProps {} - -export function SignInWithGithub(props: SignInWithGithubProps) { - return ( -
+export const SignInWithGithub = forwardRef< + HTMLButtonElement, + SignInWithIdentityProviderProps +>( + ({ children, className = "", name = "", ...props }, ref): ReactNode => ( +
- ); -} + {children ? ( + children + ) : ( + {name ? name : "Sign in with Github"} + )} + + ) +); SignInWithGithub.displayName = "SignInWithGithub"; diff --git a/packages/zitadel-react/src/components/SignInWithGitlab.tsx b/packages/zitadel-react/src/components/SignInWithGitlab.tsx index db617af45c7..b750ffeb855 100644 --- a/packages/zitadel-react/src/components/SignInWithGitlab.tsx +++ b/packages/zitadel-react/src/components/SignInWithGitlab.tsx @@ -1,18 +1,23 @@ +import { ReactNode, forwardRef } from "react"; import { SignInWithIdentityProviderProps } from "./SignInWith"; -export interface SignInWithGitlabProps - extends SignInWithIdentityProviderProps {} - -export function SignInWithGitlab(props: SignInWithGitlabProps) { - return ( -
+export const SignInWithGitlab = forwardRef< + HTMLButtonElement, + SignInWithIdentityProviderProps +>( + ({ children, className = "", name = "", ...props }, ref): ReactNode => ( +
- ); -} + {children ? ( + children + ) : ( + {name ? name : "Sign in with GitLab"} + )} + + ) +); SignInWithGitlab.displayName = "SignInWithGitlab"; diff --git a/packages/zitadel-react/src/components/SignInWithGoogle.tsx b/packages/zitadel-react/src/components/SignInWithGoogle.tsx index 0af8840e363..54e2e48b71b 100644 --- a/packages/zitadel-react/src/components/SignInWithGoogle.tsx +++ b/packages/zitadel-react/src/components/SignInWithGoogle.tsx @@ -1,23 +1,9 @@ -"use client"; - -import { - ButtonHTMLAttributes, - DetailedHTMLProps, - ReactNode, - forwardRef, -} from "react"; +import { ReactNode, forwardRef } from "react"; import { SignInWithIdentityProviderProps } from "./SignInWith"; -type SignInWithGoogleProps = DetailedHTMLProps< - ButtonHTMLAttributes, - HTMLButtonElement -> & { - name?: string; -}; - export const SignInWithGoogle = forwardRef< HTMLButtonElement, - SignInWithGoogleProps + SignInWithIdentityProviderProps >( ({ children, className = "", name = "", ...props }, ref): ReactNode => (