2022-03-06 19:46:59 -05:00
|
|
|
import { FormEvent, useState } from 'react'
|
2022-03-06 20:20:01 -05:00
|
|
|
import { Button, Card, Input, Text } from '@geist-ui/core'
|
2022-03-06 19:46:59 -05:00
|
|
|
import styles from './auth.module.css'
|
|
|
|
import { useRouter } from 'next/router'
|
2022-03-06 20:20:01 -05:00
|
|
|
import Link from '../Link'
|
2022-03-15 15:15:54 -04:00
|
|
|
import Cookies from "js-cookie";
|
2022-03-06 19:46:59 -05:00
|
|
|
|
|
|
|
const Auth = ({ page }: { page: "signup" | "signin" }) => {
|
|
|
|
const router = useRouter();
|
2022-03-12 23:13:35 -05:00
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
const [username, setUsername] = useState('');
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
|
|
|
const signingIn = page === 'signin'
|
|
|
|
|
2022-03-12 23:13:35 -05:00
|
|
|
const NO_EMPTY_SPACE_REGEX = /^\S*$/;
|
|
|
|
|
|
|
|
const handleJson = (json: any) => {
|
|
|
|
if (json.error) return setError(json.error.message)
|
|
|
|
|
2022-03-15 15:15:54 -04:00
|
|
|
Cookies.set('drift-token', json.token);
|
|
|
|
Cookies.set('drift-userid', json.userId);
|
|
|
|
|
2022-03-12 23:13:35 -05:00
|
|
|
router.push('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
|
2022-03-12 23:13:35 -05:00
|
|
|
e.preventDefault()
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-12 23:13:35 -05:00
|
|
|
if (!username.match(NO_EMPTY_SPACE_REGEX))
|
|
|
|
return setError("Username can't be empty")
|
|
|
|
|
|
|
|
if (!password.match(NO_EMPTY_SPACE_REGEX))
|
|
|
|
return setError("Password can't be empty")
|
2022-03-06 19:46:59 -05:00
|
|
|
|
|
|
|
const reqOpts = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ username, password })
|
|
|
|
}
|
|
|
|
|
2022-03-12 23:13:35 -05:00
|
|
|
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) {
|
|
|
|
setError(err.message || "Something went wrong")
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
|
|
|
<div className={styles.form}>
|
|
|
|
<div className={styles.formHeader}>
|
|
|
|
<h1>{signingIn ? 'Sign In' : 'Sign Up'}</h1>
|
|
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<Card>
|
|
|
|
<div className={styles.formGroup}>
|
|
|
|
<Input
|
|
|
|
htmlType="text"
|
|
|
|
id="username"
|
|
|
|
value={username}
|
|
|
|
onChange={(event) => setUsername(event.target.value)}
|
|
|
|
placeholder="Username"
|
|
|
|
required
|
|
|
|
label='Username'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={styles.formGroup}>
|
|
|
|
<Input
|
|
|
|
htmlType='password'
|
|
|
|
id="password"
|
|
|
|
value={password}
|
|
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
|
|
placeholder="Password"
|
|
|
|
required
|
|
|
|
label='Password'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div style={{ display: 'flex', justifyContent: 'center' }}>
|
|
|
|
<Button type="success" ghost htmlType="submit">{signingIn ? 'Sign In' : 'Sign Up'}</Button>
|
|
|
|
</div>
|
|
|
|
<div className={styles.formGroup}>
|
2022-03-12 23:13:35 -05:00
|
|
|
{signingIn ? (
|
|
|
|
<Text>
|
|
|
|
Don't have an account?{" "}
|
|
|
|
<Link color href="/signup">Sign up</Link>
|
|
|
|
</Text>
|
|
|
|
) : (
|
|
|
|
<Text>
|
|
|
|
Already have an account?{" "}
|
|
|
|
<Link color href="/signin">Sign in</Link>
|
|
|
|
</Text>
|
|
|
|
)}
|
2022-03-06 19:46:59 -05:00
|
|
|
</div>
|
|
|
|
{error && <Text type='error'>{error}</Text>}
|
|
|
|
</Card>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div >
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Auth
|