import { useState, useEffect } from 'react' import Router from 'next/router' import Link from 'next/link' import { useUser } from '../lib/hooks' export default function SignupPage() { const [user, { mutate }] = useUser() const [errorMsg, setErrorMsg] = useState('') async function onSubmit(e) { e.preventDefault() const body = { username: e.currentTarget.username.value, password: e.currentTarget.password.value, name: e.currentTarget.name.value, } if (body.password !== e.currentTarget.rpassword.value) { setErrorMsg(`The passwords don't match`) return } const res = await fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) if (res.status === 201) { const userObj = await res.json() // set user to useSWR state mutate(userObj) } else { setErrorMsg(await res.text()) } } useEffect(() => { // redirect to home if user is authenticated if (user) Router.push('/') }, [user]) return ( <>

Sign up to Example

{errorMsg &&

{errorMsg}

}
I already have an account
) }