From 04572a8e5081b1e4871e273cde9dbaaa44c52fe0 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Wed, 13 Mar 2024 21:43:44 +0000 Subject: structure: Create apps dir and copy tooling dir from t3-turbo repo --- apps/web/components/signin/CredentialsForm.tsx | 222 +++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 apps/web/components/signin/CredentialsForm.tsx (limited to 'apps/web/components/signin/CredentialsForm.tsx') diff --git a/apps/web/components/signin/CredentialsForm.tsx b/apps/web/components/signin/CredentialsForm.tsx new file mode 100644 index 00000000..5296e163 --- /dev/null +++ b/apps/web/components/signin/CredentialsForm.tsx @@ -0,0 +1,222 @@ +"use client"; +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { ActionButton } from "@/components/ui/action-button"; +import { zSignUpSchema } from "@hoarder/trpc/types/users"; +import { signIn } from "next-auth/react"; +import { useState } from "react"; +import { api } from "@/lib/trpc"; +import { useRouter } from "next/navigation"; +import { TRPCClientError } from "@trpc/client"; + +const signInSchema = z.object({ + email: z.string().email(), + password: z.string(), +}); + +function SignIn() { + const [signinError, setSigninError] = useState(false); + const router = useRouter(); + const form = useForm>({ + resolver: zodResolver(signInSchema), + }); + + return ( +
+ { + const resp = await signIn("credentials", { + redirect: false, + email: value.email, + password: value.password, + }); + if (!resp || !resp?.ok) { + setSigninError(true); + return; + } + router.replace("/"); + })} + > +
+ {signinError && ( +

+ Incorrect username or password +

+ )} + { + return ( + + Email + + + + + + ); + }} + /> + { + return ( + + Password + + + + + + ); + }} + /> + + Sign In + +
+
+ + ); +} + +function SignUp() { + const form = useForm>({ + resolver: zodResolver(zSignUpSchema), + }); + const [errorMessage, setErrorMessage] = useState(""); + + const router = useRouter(); + + const createUserMutation = api.users.create.useMutation(); + + return ( +
+ { + try { + await createUserMutation.mutateAsync(value); + } catch (e) { + if (e instanceof TRPCClientError) { + setErrorMessage(e.message); + } + return; + } + const resp = await signIn("credentials", { + redirect: false, + email: value.email, + password: value.password, + }); + if (!resp || !resp.ok) { + setErrorMessage("Hit an unexpected error while signing in"); + return; + } + router.replace("/"); + })} + > +
+ {errorMessage && ( +

{errorMessage}

+ )} + { + return ( + + Name + + + + + + ); + }} + /> + { + return ( + + Email + + + + + + ); + }} + /> + { + return ( + + Password + + + + + + ); + }} + /> + { + return ( + + Confirm Password + + + + + + ); + }} + /> + + Sign Up + +
+
+ + ); +} + +export default function CredentialsForm() { + return ( + + + Sign In + Sign Up + + + + + + + + + ); +} -- cgit v1.2.3-70-g09d2