mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
fixed issue with login that keeps returning.
This commit is contained in:
1 parent
ea38775a82
commit
11fc5c8b3d
5 files changed
+392
-142
No files matched your search
+23
-23
@@ -13,50 +13,50 @@ export default function Register() {
|
||||
|
||||
const handleRegister = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
// Basic validation
|
||||
if (!username || !password) {
|
||||
setError("Username and password are required");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
setError("");
|
||||
|
||||
try {
|
||||
// First register the user
|
||||
const response = await fetch("/api/register", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
const result = await response.json();
|
||||
setError(result.error || "Failed to register");
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
// Then try to sign in
|
||||
const signInResult = await signIn("credentials", {
|
||||
redirect: false,
|
||||
username,
|
||||
password,
|
||||
callbackUrl: "/team-selection",
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
setError(result.error);
|
||||
if (signInResult?.error) {
|
||||
setError(`Login failed: ${signInResult.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");
|
||||
}
|
||||
// Successful login
|
||||
router.push(signInResult?.url || "/team-selection");
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof SyntaxError) {
|
||||
setError("Unexpected response from server");
|
||||
} else {
|
||||
setError("An unexpected error occurred");
|
||||
}
|
||||
console.error(err);
|
||||
console.error("Registration/login error:", err);
|
||||
setError("An unexpected error occurred. Please try again.");
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user