import { useState } from "react"; import { useRouter } from "next/router"; import Link from "next/link"; import { gql } from "@apollo/client"; import { useMutation, useApolloClient } from "@apollo/client"; import { getErrorMessage } from "../lib/form"; import Field from "../components/field"; const SignInMutation = gql` mutation SignInMutation($email: String!, $password: String!) { signIn(input: { email: $email, password: $password }) { user { id email } } } `; function SignIn() { const client = useApolloClient(); const [signIn] = useMutation(SignInMutation); const [errorMsg, setErrorMsg] = useState(); const router = useRouter(); async function handleSubmit(event) { event.preventDefault(); const emailElement = event.currentTarget.elements.email; const passwordElement = event.currentTarget.elements.password; try { await client.resetStore(); const { data } = await signIn({ variables: { email: emailElement.value, password: passwordElement.value, }, }); if (data.signIn.user) { await router.push("/"); } } catch (error) { setErrorMsg(getErrorMessage(error)); } } return ( <>

Sign In

{errorMsg &&

{errorMsg}

} or{" "} Sign up ); } export default SignIn;