diff --git a/src/app/context/AuthContext.js b/src/app/context/AuthContext.js index 01881e5..6f70650 100644 --- a/src/app/context/AuthContext.js +++ b/src/app/context/AuthContext.js @@ -41,7 +41,7 @@ export const AuthProvider = ({ children }) => { if (error.code === 401) { localStorage.removeItem("isLoggedIn"); setIsAuthenticated(false); - router.push("/signup"); + router.push("/"); } } finally { setLoading(false); diff --git a/src/app/dashboard/page.js b/src/app/dashboard/page.js index 6a80d11..b16907e 100644 --- a/src/app/dashboard/page.js +++ b/src/app/dashboard/page.js @@ -13,7 +13,7 @@ export default function DashboardPage() { useEffect(() => { if (!loading && !isAuthenticated) { - router.push("/signup"); + router.push("/"); } }, [isAuthenticated, loading, router]); diff --git a/src/app/page.js b/src/app/page.js index 67ceff5..012654c 100644 --- a/src/app/page.js +++ b/src/app/page.js @@ -1,32 +1,152 @@ -// src/app/page.js -"use client"; // Mark this as a client component - +"use client"; import { useRouter } from "next/navigation"; -import { useAuth } from "./context/AuthContext"; // Import useAuth -import { useEffect } from "react"; // Import useEffect +import { useAuth } from "./context/AuthContext"; +import { useState, useEffect } from "react"; +import Link from "next/link"; +import Button1 from "./ui/buttons/button1"; +import { account, ID } from "./lib/appwrite"; -export default function Home() { +export default function AuthPage() { const router = useRouter(); - const { isAuthenticated, loading } = useAuth(); + const { isAuthenticated, loading: authLoading, setIsAuthenticated } = useAuth(); + const [isLoginForm, setIsLoginForm] = useState(true); + const [darkMode, setDarkMode] = useState(false); + const [loading, setLoading] = useState(false); + const [showPassword, setShowPassword] = useState(false); + + // Form states + const [loginData, setLoginData] = useState({ + email: "", + password: "", + rememberMe: false, + }); + + const [signupData, setSignupData] = useState({ + fname: "", + lname: "", + email: "", + password: "", + agreeTerms: false, + }); + + const [errors, setErrors] = useState({ + agreeTerms: "", + general: "", + }); useEffect(() => { - if (!loading) { - if (isAuthenticated) { - router.push("/dashboard"); - } else { - router.push("/signup"); - } + if (!authLoading && isAuthenticated) { + router.push("/dashboard"); } - }, [isAuthenticated, loading, router]); + }, [isAuthenticated, authLoading, router]); - if (loading) { + const handleLoginChange = (e) => { + const { name, value, type, checked } = e.target; + setLoginData({ + ...loginData, + [name]: type === "checkbox" ? checked : value, + }); + }; + + const handleSignupChange = (e) => { + const { name, value, type, checked } = e.target; + setSignupData({ + ...signupData, + [name]: type === "checkbox" ? checked : value, + }); + if (name === "agreeTerms") { + setErrors({ ...errors, agreeTerms: "" }); + } + }; + + const validateSignupForm = () => { + let isValid = true; + const newErrors = { ...errors }; + + if (!signupData.agreeTerms) { + newErrors.agreeTerms = "You must agree to the terms and conditions"; + isValid = false; + } else { + newErrors.agreeTerms = ""; + } + + setErrors(newErrors); + return isValid; + }; + + const handleLogin = async (e) => { + e.preventDefault(); + setLoading(true); + setErrors({ ...errors, general: "" }); + + try { + const session = await account.createEmailPasswordSession( + loginData.email, + loginData.password + ); + + if (session) { + const user = await account.get(); + if (user) { + localStorage.setItem("isLoggedIn", "true"); + setIsAuthenticated(true); + router.push("/dashboard"); + } + } + } catch (error) { + let errorMessage = 'Login failed. Please try again.'; + if (error.type === 'user_invalid_credentials') { + errorMessage = 'Invalid email or password'; + } else if (error.type === 'general_argument_invalid') { + errorMessage = 'Invalid email format'; + } + setErrors({ ...errors, general: errorMessage }); + } finally { + setLoading(false); + } + }; + + const handleSignup = async (e) => { + e.preventDefault(); + + if (!validateSignupForm()) { + return; + } + + setLoading(true); + setErrors({ ...errors, general: "" }); + + try { + await account.create( + ID.unique(), + signupData.email, + signupData.password, + `${signupData.fname} ${signupData.lname}` + ); + await account.createEmailPasswordSession(signupData.email, signupData.password); + const user = await account.get(); + if (user) { + localStorage.setItem("isLoggedIn", "true"); + setIsAuthenticated(true); + router.push("/dashboard"); + } + } catch (error) { + console.error("Signup error:", error); + setErrors({ + ...errors, + general: error.message || "Signup failed. Please try again.", + }); + } finally { + setLoading(false); + } + }; + + if (authLoading) { return (
- {/* Spinner */}
- {/* Loading Text */} -

+

Loading...

@@ -34,5 +154,231 @@ export default function Home() { ); } - return null; // No need to render anything here + if (isAuthenticated) { + return null; + } + + return ( +
+
+
+
+

+ {isLoginForm ? "Sign In" : "Sign Up"} +

+

+ {isLoginForm + ? "Enter your email and password to sign In!" + : "Enter your deatils to create an account!"} +

+
+ + {errors.general && ( +
+ {errors.general} +
+ )} + + {isLoginForm ? ( + // Login Form +
+
+ + +
+ +
+ +
+ + +
+
+ +
+ + + + Forgot password? + +
+ + + {loading ? "Signing in..." : "Sign in"} + +
+ ) : ( + // Sign Up Form +
+
+
+ + +
+ +
+ + +
+
+ +
+ + +
+ +
+ +
+ + +
+
+ +
+ + {errors.agreeTerms && ( +

+ {errors.agreeTerms} +

+ )} +
+ + + {loading ? "Creating account..." : "Sign up"} + +
+ )} + +
+

+ {isLoginForm ? "Don't have an account?" : "Already have an account?"} + +

+
+
+
+ + {/* Dark mode toggle */} + +
+ ); } \ No newline at end of file