mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
162 lines
4.4 KiB
JavaScript
162 lines
4.4 KiB
JavaScript
"use client"; // Mark as a Client Component
|
|
import { useState } from "react";
|
|
import { signIn } from "next-auth/react";
|
|
import { useRouter } from "next/navigation";
|
|
import Link from "next/link";
|
|
|
|
export default function Register() {
|
|
const [username, setUsername] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const router = useRouter();
|
|
|
|
const handleRegister = async (e) => {
|
|
e.preventDefault();
|
|
setIsLoading(true);
|
|
setError("");
|
|
|
|
try {
|
|
const response = await fetch("/api/register", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password }),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const result = await response.json();
|
|
setError(result.error || "Failed to register");
|
|
setIsLoading(false);
|
|
return;
|
|
}
|
|
|
|
const result = await response.json();
|
|
|
|
if (result.error) {
|
|
setError(result.error);
|
|
setIsLoading(false);
|
|
} else {
|
|
// Automatically log in the user after successful registration
|
|
const signInResult = await signIn("credentials", {
|
|
redirect: false,
|
|
username,
|
|
password,
|
|
});
|
|
|
|
if (signInResult?.error) {
|
|
setError("Registration successful, but failed to log in");
|
|
setIsLoading(false);
|
|
} else {
|
|
router.push("/team-selection");
|
|
}
|
|
}
|
|
} catch (err) {
|
|
if (err instanceof SyntaxError) {
|
|
setError("Unexpected response from server");
|
|
} else {
|
|
setError("An unexpected error occurred");
|
|
}
|
|
console.error(err);
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="auth-page">
|
|
<div className="page-header">
|
|
<div className="back-button">←</div>
|
|
<div className="page-title">Register</div>
|
|
<div className="placeholder"></div>
|
|
</div>
|
|
|
|
<div className="auth-container">
|
|
<div className="auth-card">
|
|
<div className="input-container">
|
|
<input
|
|
type="text"
|
|
placeholder="Username"
|
|
value={username}
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
required
|
|
className="auth-input"
|
|
/>
|
|
</div>
|
|
<div className="input-container">
|
|
<input
|
|
type="password"
|
|
placeholder="Password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
className="auth-input"
|
|
/>
|
|
</div>
|
|
{error && <div className="error-message">{error}</div>}
|
|
<button
|
|
onClick={handleRegister}
|
|
className="auth-button"
|
|
disabled={isLoading}
|
|
>
|
|
{isLoading ? "Registering..." : "Register"}
|
|
</button>
|
|
<Link href="/login">
|
|
<button className="auth-button">Log In</button>
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
|
|
<style jsx>{`
|
|
.auth-page {
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
align-items: center;
|
|
height: 100vh;
|
|
overflow: hidden;
|
|
}
|
|
.auth-container {
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
align-items: center;
|
|
width: 100%;
|
|
max-width: 400px;
|
|
padding: 20px;
|
|
box-sizing: border-box;
|
|
}
|
|
.auth-card {
|
|
width: 100%;
|
|
padding: 20px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
border-radius: 8px;
|
|
background-color: #fff;
|
|
}
|
|
.input-container {
|
|
margin-bottom: 10px;
|
|
}
|
|
.auth-input {
|
|
width: 100%;
|
|
padding: 10px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 4px;
|
|
}
|
|
.auth-button {
|
|
width: 100%;
|
|
padding: 10px;
|
|
margin-top: 10px;
|
|
background-color: #68543c; /* Updated brown color */
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
text-align: center;
|
|
}
|
|
.error-message {
|
|
color: red;
|
|
margin-top: 10px;
|
|
}
|
|
`}</style>
|
|
</div>
|
|
);
|
|
}
|