"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { account, ID } from "../../lib/appwrite"; import { useAuth } from "../../context/AuthContext"; import Button1 from "../../ui/buttons/button1"; export default function SignUpPage() { const [darkMode, setDarkMode] = useState(false); const [formData, setFormData] = useState({ fname: "", lname: "", email: "", password: "", agreeTerms: false, }); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const router = useRouter(); const { setIsAuthenticated } = useAuth(); useEffect(() => { const checkAuth = async () => { try { const user = await account.get(); if (user) { localStorage.setItem("isLoggedIn", "true"); router.push("/dashboard"); } } catch (error) { console.warn("No active session:", error); localStorage.removeItem("isLoggedIn"); } }; checkAuth(); }, [router]); const handleChange = (e) => { const { name, value, type, checked } = e.target; setFormData({ ...formData, [name]: type === "checkbox" ? checked : value, }); }; const handleSubmit = async (e) => { e.preventDefault(); if (!formData.agreeTerms) { alert("You must agree to the terms and conditions"); return; } setLoading(true); try { // Create user account await account.create( ID.unique(), formData.email, formData.password, `${formData.fname} ${formData.lname}` ); // Create session immediately after signup await account.createEmailPasswordSession(formData.email, formData.password); const user = await account.get(); if (user) { localStorage.setItem("isLoggedIn", "true"); setIsAuthenticated(true); router.push("/dashboard"); } } catch (error) { console.error("Signup error:", error); alert(`Signup failed: ${error.message}`); } finally { setLoading(false); } }; return (
Enter your email and password to sign up!
Already have an account? Sign In