125 lines
4.9 KiB
TypeScript
125 lines
4.9 KiB
TypeScript
'use client';
|
|
|
|
import Link from "next/link";
|
|
import { useState, useEffect } from "react";
|
|
import { register } from "../../lib/client/register";
|
|
import { useRouter } from "next/navigation"
|
|
|
|
export default function RegisterPage() {
|
|
const [name, setName] = useState(""); // creating state objects and getting setters
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [confirmPassword, setConfirmPassword] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const router = useRouter();
|
|
|
|
const handleSubmit = async (e: React.SubmitEvent<HTMLFormElement>) => { // event object from form element
|
|
e.preventDefault(); // stops default submission to send through the js instead
|
|
setError(""); // clear state
|
|
|
|
if (password !== confirmPassword) {
|
|
setError("Passwords do not match")
|
|
return;
|
|
}
|
|
|
|
if (password.length < 16 || password === password.toLowerCase() || password === password.toUpperCase()) { // rejects password if its not 16+ characters or if its all in the same case
|
|
setError("Password must be at least 16 characters and a mix of lower and upper case. To keep your account secure also include special characters and dont use predictable phrases.")
|
|
return;
|
|
}
|
|
|
|
setIsSubmitting(true); // once password is valid
|
|
|
|
try {
|
|
await register({ email, password, name })
|
|
router.push("/login?registered=true")
|
|
} catch (error) {
|
|
setError(error instanceof Error ? error.message : "Registration failed")
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
}
|
|
|
|
// useEffect(() => { // on page load
|
|
// if (!isLoading && isAuthenticated) {
|
|
// router.push("/")
|
|
// }
|
|
// },[isLoading, isAuthenticated, router])
|
|
|
|
return (
|
|
<>
|
|
|
|
<h2 className="card-title text-2xl justify-center">Create Account</h2>
|
|
<form onSubmit={handleSubmit} className="mt-4">
|
|
{error && (
|
|
<div className="alert alert-error mb-4">
|
|
<span>{error}</span>
|
|
</div>
|
|
)}
|
|
{" "}
|
|
<div className="form-control mt-4">
|
|
<label className="label">
|
|
<span className="label-text">Name</span>
|
|
</label>
|
|
<input
|
|
type="text"
|
|
placeholder="Enter your name"
|
|
className="input input-bordered w-full"
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="form-control mt-4">
|
|
<label className="label">
|
|
<span className="label-text">Email</span>
|
|
</label>
|
|
<input
|
|
type="email"
|
|
placeholder="Enter your email"
|
|
className="input input-bordered w-full"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="form-control mt-4">
|
|
<label className="label">
|
|
<span className="label-text">Password</span>
|
|
</label>
|
|
<input
|
|
type="password"
|
|
placeholder="Enter your password"
|
|
className="input input-bordered w-full"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="form-control mt-4">
|
|
<label className="label">
|
|
<span className="label-text">Confirm Password</span>
|
|
</label>
|
|
<input
|
|
type="password"
|
|
placeholder="Confirm your password"
|
|
className="input input-bordered w-full"
|
|
value={confirmPassword}
|
|
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div className="form-control mt-6">
|
|
<button type="submit" className="btn btn-primary w-full" disabled={isSubmitting}>
|
|
{isSubmitting ? (
|
|
<span className="loading loading-spinner loading-sm"></span>
|
|
) : (
|
|
"Create Account"
|
|
)}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</>
|
|
)
|
|
};
|