Skip to content

Instantly share code, notes, and snippets.

@Olanetsoft
Last active November 24, 2022 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Olanetsoft/48d1da1ff3c7e1afe2226446f9b5a88a to your computer and use it in GitHub Desktop.
Save Olanetsoft/48d1da1ff3c7e1afe2226446f9b5a88a to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
import { useRouter } from "next/router";
import Navbar from "../components/Navbar";
const Register = () => {
const router = useRouter();
const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
console.log("Submitting form");
console.log(email, password, firstName, lastName);
await fetch("/api/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
firstName,
lastName,
email,
password,
}),
})
.then((res) => res.json())
.then((data) => {
console.log(data);
if (data.error) {
setError(data.error);
} else {
router.push("/login");
}
});
setLoading(false);
};
return (
<div className="container mx-auto flex justify-center items-center h-screen">
<div className="w-full max-w-xs bg-white rounded-lg shadow-md overflow-hidden mx-10">
<h1 className="text-3xl font-bold text-center text-gray-700 mb-4 mt-6">
Register
</h1>
<form
className="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4"
onSubmit={handleSubmit}
>
<div className="mb-4">
<input
className="form-control block w-full px-2 py-2 text-l font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none mt-2 mb-8"
type="text"
name="firstName"
placeholder="First Name"
value={firstName}
onChange={(e) => setFirstName(e.target.value)}
required
/>
<input
className="form-control block w-full px-2 py-2 text-l font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none mt-2 mb-8"
type="text"
name="lastName"
placeholder="Last Name"
value={lastName}
onChange={(e) => setLastName(e.target.value)}
required
/>
<input
className="form-control block w-full px-2 py-2 text-l font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none mt-2 mb-8"
id="email"
type="email"
placeholder="Email"
onChange={(e) => setEmail(e.target.value)}
value={email}
required
/>
<input
className="form-control block w-full px-2 py-2 text-l font-normal text-gray-700 bg-white bg-clip-padding border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none mt-2 mb-6"
id="password"
type="password"
placeholder="Password"
onChange={(e) => setPassword(e.target.value)}
value={password}
required
/>
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline mt-4 mb-4"
type="submit"
disabled={loading}
>
{loading ? "Loading..." : "Register"}
</button>
{error && <p className="text-red-500">{error}</p>}
<p className="text-center text-gray-500 text-xs">
<a
className="text-blue-500 hover:text-blue-700 no-underline hover:underline cursor-pointer text-md mt-6"
href="/login"
>
Already have an account? Login here.
</a>
</p>
</div>
</form>
</div>
</div>
);
};
export default Register;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment