rsnext/examples/api-routes-apollo-server-and-client-auth/pages/signin.js
Luis Alvarez D 9dc902835c
[Examples] Move api-routes-apollo-server-and-client-auth to SSG (#13849)
Related to https://github.com/vercel/next.js/issues/11014

The authentication method has been replaced with an improved version of the passport example.
2020-06-10 03:46:11 +00:00

76 lines
1.8 KiB
JavaScript

import { useState } from 'react'
import { useRouter } from 'next/router'
import Link from 'next/link'
import gql from 'graphql-tag'
import { useMutation, useApolloClient } from '@apollo/react-hooks'
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 (
<>
<h1>Sign In</h1>
<form onSubmit={handleSubmit}>
{errorMsg && <p>{errorMsg}</p>}
<Field
name="email"
type="email"
autoComplete="email"
required
label="Email"
/>
<Field
name="password"
type="password"
autoComplete="password"
required
label="Password"
/>
<button type="submit">Sign in</button> or{' '}
<Link href="signup">
<a>Sign up</a>
</Link>
</form>
</>
)
}
export default SignIn