diff --git a/apps/console/src/pages/iam/auth/sign-in/SignInPage.tsx b/apps/console/src/pages/iam/auth/sign-in/SignInPage.tsx index b932b8b54..adeaba99e 100644 --- a/apps/console/src/pages/iam/auth/sign-in/SignInPage.tsx +++ b/apps/console/src/pages/iam/auth/sign-in/SignInPage.tsx @@ -1,15 +1,28 @@ +import { formatError, type GraphQLError } from "@probo/helpers"; import { useTranslate } from "@probo/i18n"; -import { Button } from "@probo/ui"; -import { useLazyLoadQuery } from "react-relay"; -import { Link, useLocation } from "react-router"; +import { Button, Field, useToast } from "@probo/ui"; +import type { FormEventHandler } from "react"; +import { useLazyLoadQuery, useMutation } from "react-relay"; +import { Link, matchPath, useLocation } from "react-router"; import { graphql } from "relay-runtime"; +import type { SignInPageMutation } from "#/__generated__/iam/SignInPageMutation.graphql"; import type { SignInPageQuery } from "#/__generated__/iam/SignInPageQuery.graphql"; import { useSafeContinueUrl } from "#/hooks/useSafeContinueUrl"; import { Divider } from "./_components/Divider"; import { OIDCButtons } from "./_components/OIDCButtons"; +const signInMutation = graphql` + mutation SignInPageMutation($input: SignInInput!) { + signIn(input: $input) { + session { + id + } + } + } +`; + const signInPageQuery = graphql` query SignInPageQuery { oidcProviders { @@ -20,35 +33,113 @@ const signInPageQuery = graphql` export default function SignInPage() { const { __ } = useTranslate(); + const { toast } = useToast(); const location = useLocation(); const safeContinueUrl = useSafeContinueUrl(); const data = useLazyLoadQuery(signInPageQuery, {}); + const [signIn, isSigningIn] + = useMutation(signInMutation); + + const handleSubmit: FormEventHandler = (e) => { + e.preventDefault(); + const formData = new FormData(e.currentTarget); + const email = (formData.get("email") as string) ?? ""; + const password = (formData.get("password") as string) ?? ""; + + if (!email || !password) return; + + const match = matchPath( + { + path: "/organizations/:organizationId", + caseSensitive: false, + end: false, + }, + safeContinueUrl.pathname, + ); + + signIn({ + variables: { + input: { + email, + password, + organizationId: match?.params.organizationId ?? null, + }, + }, + onCompleted: (_, error) => { + if (error) { + toast({ + title: __("Error"), + description: formatError( + __("Failed to sign in"), + error as GraphQLError, + ), + variant: "error", + }); + return; + } + + window.location.href = safeContinueUrl.href; + }, + onError: (e) => { + toast({ + title: __("Error"), + description: e.message, + variant: "error", + }); + }, + }); + }; + return (

{__("Sign in to your account")}

+
+ + +
+
+ + + {__("Forgot your password?")} + +
+ +
+ + + +
+ {__("Or")} + - {data.oidcProviders.length > 0 && ( - {__("Or")} - )} - - -