|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { useMutation } from "@tanstack/react-query"; |
| 8 | +import { FC, useCallback, useState } from "react"; |
| 9 | +import { Button } from "../components/Button"; |
| 10 | +import { TextInputField } from "../components/forms/TextInputField"; |
| 11 | + |
| 12 | +export const SSOLoginForm: FC = () => { |
| 13 | + const [orgSlug, setOrgSlug] = useState(""); |
| 14 | + // TODO: remove this |
| 15 | + const [loginUrl, setLoginUrl] = useState(""); |
| 16 | + |
| 17 | + const exchangeSlug = useMutation({ |
| 18 | + mutationFn: async ({ slug }: { slug: string }) => { |
| 19 | + // make api call to get provider id by slug |
| 20 | + // return provider id |
| 21 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 22 | + |
| 23 | + return { id: "sample-id" }; |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + const handleSSOLogin = useCallback( |
| 28 | + async (e) => { |
| 29 | + e.preventDefault(); |
| 30 | + |
| 31 | + // make api call to get provider id by slug |
| 32 | + const { id } = await exchangeSlug.mutateAsync({ slug: orgSlug }); |
| 33 | + |
| 34 | + // create sso login url with provider id |
| 35 | + const loginUrl = `/oidc/start/?id=${id}`; |
| 36 | + setLoginUrl(loginUrl); |
| 37 | + |
| 38 | + // openAuthorize window for sso w/ login url |
| 39 | + }, |
| 40 | + [exchangeSlug, orgSlug], |
| 41 | + ); |
| 42 | + |
| 43 | + // TODO: Wrap with feature flag check |
| 44 | + return ( |
| 45 | + <form onSubmit={handleSSOLogin}> |
| 46 | + <div className="mt-10 space-y-2"> |
| 47 | + <TextInputField label="Organization Slug" value={orgSlug} onChange={setOrgSlug} /> |
| 48 | + <Button className="w-full" type="secondary" disabled={!orgSlug} loading={exchangeSlug.isLoading}> |
| 49 | + Continue with SSO |
| 50 | + </Button> |
| 51 | + {loginUrl && <p>{loginUrl}</p>} |
| 52 | + </div> |
| 53 | + </form> |
| 54 | + ); |
| 55 | +}; |
0 commit comments