mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +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
+42
-25
@@ -1,8 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { signIn } from "next-auth/react";
|
||||
import { useRouter } from "next/navigation";
|
||||
"use client"; // Mark as a Client Component
|
||||
import { useState, useEffect } from "react";
|
||||
import { signIn, useSession } from "next-auth/react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
|
||||
export default function Login() {
|
||||
@@ -10,39 +9,57 @@ export default function Login() {
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const router = useRouter();
|
||||
const { data: session, status } = useSession();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
// Redirect if already authenticated
|
||||
useEffect(() => {
|
||||
if (status === "authenticated") {
|
||||
const callbackUrl = searchParams.get("callbackUrl") || "/team-selection";
|
||||
router.push(callbackUrl);
|
||||
}
|
||||
|
||||
// Check for error param
|
||||
const errorParam = searchParams.get("error");
|
||||
if (errorParam) {
|
||||
if (errorParam === "AuthError") {
|
||||
setError("Session expired. Please log in again.");
|
||||
} else {
|
||||
setError("Authentication failed. Please try again.");
|
||||
}
|
||||
}
|
||||
}, [status, router, searchParams]);
|
||||
|
||||
const handleLogin = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!username || !password) {
|
||||
setError("Username and password are required");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
setError("");
|
||||
|
||||
try {
|
||||
console.log("Attempting to sign in with:", username);
|
||||
|
||||
const result = await signIn("credentials", {
|
||||
redirect: false,
|
||||
username,
|
||||
password,
|
||||
callbackUrl: `${window.location.origin}/team-selection`,
|
||||
callbackUrl: searchParams.get("callbackUrl") || "/team-selection",
|
||||
});
|
||||
|
||||
console.log("Sign in result:", result);
|
||||
|
||||
if (result?.error) {
|
||||
throw new Error(result.error);
|
||||
}
|
||||
|
||||
if (result?.ok) {
|
||||
console.log("Sign in successful, redirecting...");
|
||||
// Use router.push with { replace: true } to avoid history issues
|
||||
router.push("/team-selection", { replace: true });
|
||||
return;
|
||||
setError(result.error || "Login failed");
|
||||
setIsLoading(false);
|
||||
} else if (result?.url) {
|
||||
router.push(result.url);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
setError(error.message || "Failed to login. Please try again.");
|
||||
} finally {
|
||||
setError("Connection error. Please try again.");
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
@@ -79,7 +96,7 @@ export default function Login() {
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{/* Add separate div for register button with margin-top */}
|
||||
{/* Reduced margin in the container */}
|
||||
<div className="register-button-container">
|
||||
<Link href="/register">
|
||||
<button className="auth-button register-button">Register</button>
|
||||
@@ -88,7 +105,7 @@ export default function Login() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Update styles to include spacing between buttons */}
|
||||
{/* Update styles to reduce gap */}
|
||||
<style jsx>{`
|
||||
.auth-card {
|
||||
background-color: #fff;
|
||||
@@ -100,13 +117,13 @@ export default function Login() {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
margin-bottom: 8px; /* Reduced margin below the form (was 15px) */
|
||||
margin-bottom: 2px; /* Reduced space after form */
|
||||
}
|
||||
.input-container {
|
||||
margin-bottom: 10px; /* Add consistent spacing between input fields */
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.register-button-container {
|
||||
margin-top: 5px; /* Reduced margin above register button (was 10px) */
|
||||
margin-top: 2px; /* Reduced space before register button */
|
||||
}
|
||||
.auth-button {
|
||||
padding: 10px;
|
||||
|
||||
+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