import { FormEvent, useState } from 'react' import { Button, Input, Text, useToasts } from '@geist-ui/core' import styles from './auth.module.css' import { useRouter } from 'next/router' import Link from '../Link' const Auth = ({ page }: { page: "signup" | "signin" }) => { const router = useRouter(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const { setToast } = useToasts(); const signingIn = page === 'signin' const handleJson = (json: any) => { localStorage.setItem('drift-token', json.token) localStorage.setItem('drift-userid', json.userId) router.push('/') } const handleSubmit = async (e: FormEvent) => { e.preventDefault() const reqOpts = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) } try { const signUrl = signingIn ? '/server-api/auth/signin' : '/server-api/auth/signup'; const resp = await fetch(signUrl, reqOpts); const json = await resp.json(); if (!resp.ok) throw new Error(); handleJson(json) } catch (err: any) { setToast({ text: "Something went wrong", type: 'error' }) } } return (

{signingIn ? 'Sign In' : 'Sign Up'}

setUsername(event.target.value)} placeholder="Username" required scale={4 / 3} /> setPassword(event.target.value)} placeholder="Password" required scale={4 / 3} />
{signingIn ? ( Don't have an account?{" "} Sign up ) : ( Already have an account?{" "} Sign in )}
) } export default Auth