2022-11-09 21:38:05 -05:00
|
|
|
"use client"
|
|
|
|
|
2022-11-10 02:11:36 -05:00
|
|
|
import { FormEvent, useState } from "react"
|
2022-04-09 20:48:19 -04:00
|
|
|
import styles from "./auth.module.css"
|
2022-11-09 21:38:05 -05:00
|
|
|
import { useRouter } from "next/navigation"
|
2022-11-08 03:23:28 -05:00
|
|
|
import Link from "../link"
|
2022-11-10 02:11:36 -05:00
|
|
|
import { Button, Input, Note } from "@geist-ui/core/dist"
|
2022-11-11 22:17:44 -05:00
|
|
|
import { signIn } from "next-auth/react"
|
|
|
|
import { Github as GithubIcon } from "@geist-ui/icons"
|
2022-04-09 20:48:19 -04:00
|
|
|
const NO_EMPTY_SPACE_REGEX = /^\S*$/
|
|
|
|
const ERROR_MESSAGE =
|
|
|
|
"Provide a non empty username and a password with at least 6 characters"
|
2022-03-13 15:57:55 -04:00
|
|
|
|
2022-11-09 21:38:05 -05:00
|
|
|
const Auth = ({
|
|
|
|
page,
|
|
|
|
requiresServerPassword
|
|
|
|
}: {
|
|
|
|
page: "signup" | "signin"
|
|
|
|
requiresServerPassword?: boolean
|
|
|
|
}) => {
|
2022-04-09 20:48:19 -04:00
|
|
|
const router = useRouter()
|
2022-03-12 23:13:35 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const [username, setUsername] = useState("")
|
|
|
|
const [password, setPassword] = useState("")
|
|
|
|
const [serverPassword, setServerPassword] = useState("")
|
|
|
|
const [errorMsg, setErrorMsg] = useState("")
|
|
|
|
const signingIn = page === "signin"
|
2022-03-21 02:27:09 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const handleJson = (json: any) => {
|
2022-11-11 22:17:44 -05:00
|
|
|
// setCookie(USER_COOKIE_NAME, json.userId)
|
2022-03-21 02:27:09 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
router.push("/new")
|
|
|
|
}
|
2022-03-15 15:15:54 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
|
|
|
|
e.preventDefault()
|
2022-11-11 22:17:44 -05:00
|
|
|
// if (
|
|
|
|
// !signingIn &&
|
|
|
|
// (!NO_EMPTY_SPACE_REGEX.test(username) || password.length < 6)
|
|
|
|
// )
|
|
|
|
// return setErrorMsg(ERROR_MESSAGE)
|
|
|
|
// if (
|
|
|
|
// !signingIn &&
|
|
|
|
// requiresServerPassword &&
|
|
|
|
// !NO_EMPTY_SPACE_REGEX.test(serverPassword)
|
|
|
|
// )
|
|
|
|
// return setErrorMsg(ERROR_MESSAGE)
|
|
|
|
// else setErrorMsg("")
|
2022-03-12 23:13:35 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const reqOpts = {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ username, password, serverPassword })
|
|
|
|
}
|
2022-03-13 15:57:55 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
try {
|
2022-11-11 22:17:44 -05:00
|
|
|
// signIn("credentials", {
|
|
|
|
// callbackUrl: "/new",
|
|
|
|
// redirect: false,
|
|
|
|
// username,
|
|
|
|
// password,
|
|
|
|
// serverPassword
|
|
|
|
// })
|
|
|
|
// const signUrl = signingIn ? "/api/auth/signin" : "/api/auth/signup"
|
|
|
|
// const resp = await fetch(signUrl, reqOpts)
|
|
|
|
// const json = await resp.json()
|
|
|
|
// if (!resp.ok) throw new Error(json.error.message)
|
|
|
|
// handleJson(json)
|
2022-04-09 20:48:19 -04:00
|
|
|
} catch (err: any) {
|
|
|
|
setErrorMsg(err.message ?? "Something went wrong")
|
|
|
|
}
|
|
|
|
}
|
2022-03-12 23:13:35 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
|
|
|
<div className={styles.form}>
|
|
|
|
<div className={styles.formContentSpace}>
|
|
|
|
<h1>{signingIn ? "Sign In" : "Sign Up"}</h1>
|
|
|
|
</div>
|
2022-11-11 22:17:44 -05:00
|
|
|
{/* <form onSubmit={handleSubmit}> */}
|
|
|
|
<form>
|
2022-04-09 20:48:19 -04:00
|
|
|
<div className={styles.formGroup}>
|
2022-11-11 22:17:44 -05:00
|
|
|
{/* <Input
|
2022-11-10 02:11:36 -05:00
|
|
|
htmlType="text"
|
2022-04-09 20:48:19 -04:00
|
|
|
id="username"
|
|
|
|
value={username}
|
2022-11-08 03:23:28 -05:00
|
|
|
onChange={(event) => setUsername(event.currentTarget.value)}
|
2022-04-09 20:48:19 -04:00
|
|
|
placeholder="Username"
|
|
|
|
required
|
2022-11-10 02:11:36 -05:00
|
|
|
minLength={3}
|
2022-11-11 22:17:44 -05:00
|
|
|
width="100%"
|
2022-04-09 20:48:19 -04:00
|
|
|
/>
|
|
|
|
<Input
|
2022-11-10 02:11:36 -05:00
|
|
|
htmlType="password"
|
2022-04-09 20:48:19 -04:00
|
|
|
id="password"
|
|
|
|
value={password}
|
2022-11-08 03:23:28 -05:00
|
|
|
onChange={(event) => setPassword(event.currentTarget.value)}
|
2022-04-09 20:48:19 -04:00
|
|
|
placeholder="Password"
|
|
|
|
required
|
2022-11-10 02:11:36 -05:00
|
|
|
minLength={6}
|
2022-11-11 22:17:44 -05:00
|
|
|
width="100%"
|
|
|
|
/> */}
|
|
|
|
{/* sign in with github */}
|
2022-04-09 20:48:19 -04:00
|
|
|
{requiresServerPassword && (
|
|
|
|
<Input
|
2022-11-10 02:11:36 -05:00
|
|
|
htmlType="password"
|
2022-04-09 20:48:19 -04:00
|
|
|
id="server-password"
|
|
|
|
value={serverPassword}
|
2022-11-08 03:23:28 -05:00
|
|
|
onChange={(event) =>
|
|
|
|
setServerPassword(event.currentTarget.value)
|
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
placeholder="Server Password"
|
|
|
|
required
|
2022-11-10 02:11:36 -05:00
|
|
|
width="100%"
|
2022-04-09 20:48:19 -04:00
|
|
|
/>
|
|
|
|
)}
|
2022-11-11 22:17:44 -05:00
|
|
|
<Button
|
|
|
|
htmlType="submit"
|
|
|
|
type="success-light"
|
|
|
|
auto
|
|
|
|
width="100%"
|
|
|
|
icon={<GithubIcon />}
|
|
|
|
onClick={() => signIn("github")}
|
|
|
|
>
|
|
|
|
Sign in with GitHub
|
|
|
|
</Button>
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
{/* <Button width={"100%"} htmlType="submit">
|
2022-04-09 20:48:19 -04:00
|
|
|
{signingIn ? "Sign In" : "Sign Up"}
|
2022-11-11 22:17:44 -05:00
|
|
|
</Button> */}
|
2022-04-09 20:48:19 -04:00
|
|
|
</div>
|
|
|
|
<div className={styles.formContentSpace}>
|
|
|
|
{signingIn ? (
|
2022-11-08 03:23:28 -05:00
|
|
|
<p>
|
2022-04-09 20:48:19 -04:00
|
|
|
Don't have an account?{" "}
|
2022-11-08 03:23:28 -05:00
|
|
|
<Link colored href="/signup">
|
2022-04-09 20:48:19 -04:00
|
|
|
Sign up
|
|
|
|
</Link>
|
2022-11-08 03:23:28 -05:00
|
|
|
</p>
|
2022-04-09 20:48:19 -04:00
|
|
|
) : (
|
2022-11-08 03:23:28 -05:00
|
|
|
<p>
|
2022-11-11 22:17:44 -05:00
|
|
|
Have an account?{" "}
|
2022-11-08 03:23:28 -05:00
|
|
|
<Link colored href="/signin">
|
2022-04-09 20:48:19 -04:00
|
|
|
Sign in
|
|
|
|
</Link>
|
2022-11-08 03:23:28 -05:00
|
|
|
</p>
|
2022-04-09 20:48:19 -04:00
|
|
|
)}
|
|
|
|
</div>
|
2022-11-09 21:38:05 -05:00
|
|
|
{errorMsg && <Note type="error">{errorMsg}</Note>}
|
2022-04-09 20:48:19 -04:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
export default Auth
|