From 2a9b4f49b8ffa002673780e9b83ef92c24344846 Mon Sep 17 00:00:00 2001 From: LaidBenglia <73167661+LaidBengli@users.noreply.github.com> Date: Wed, 1 Nov 2023 04:13:45 -0700 Subject: [PATCH] feat: add signup function to the component --- src/components/signup/SignUpForm.jsx | 103 +++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 5 deletions(-) diff --git a/src/components/signup/SignUpForm.jsx b/src/components/signup/SignUpForm.jsx index 8ae0f51..355be42 100644 --- a/src/components/signup/SignUpForm.jsx +++ b/src/components/signup/SignUpForm.jsx @@ -1,4 +1,72 @@ +import firebase from "firebase/app"; // Import Firebase app +import React, { useEffect, useState } from "react"; +import "firebase/auth"; // Import Firebase Authentication +import "firebase/firestore"; // Import Firestore +import "firebase/storage"; // Import Firebase Storage +import { setDoc } from "@/lib/firebase/firestoreFunctions"; +import auth from "@/lib/firebase/firebase"; + +import { useAuth } from "@/context/AuthContext"; + const SignUpForm = () => { + const { signup } = useAuth(); + const [formData, setFormData] = useState({ + name: "", + email: "", + password: "", + confirmPassword: "", + phone: "", + location: "", + }); + const [firebaseInitialized, setFirebaseInitialized] = useState(true); + + const handleChange = (e) => { + const { name, value } = e.target; + setFormData({ + ...formData, + [name]: value, + }); + }; + + const handleSignUp = async () => { + try { + if (!firebaseInitialized) { + console.log("Firebase is not yet initialized. Please wait."); + return; + } + const { name, email, password, phone, location } = formData; + + // Create a user with email and password + const userCredential = await signup( + formData.email, + formData.password, + ); + + const user = userCredential.user; + const db = firebase.firestore(); // Initialize Firestore (if not already done in 'firebase.js') + + await db.collection("users").doc(user.uid).set({ + name, + email, + phone, + location, + }); + + // reset the form + setFormData({ + name: "", + email: "", + password: "", + confirmPassword: "", + phone: "", + location: "", + }); + + console.log("User signed up successfully."); + } catch (error) { + console.error("Error signin up:", error.message); + } + }; return (
@@ -9,9 +77,12 @@ const SignUpForm = () => { Name @@ -24,6 +95,9 @@ const SignUpForm = () => { className='rounded-2xl border border-[#d3d5e2] mt-1 w-full p-3 placeholder-[#9CA2A9] shadow-md text-sm' type='text' id='email' + name='email' + value={formData.email} + onChange={handleChange} placeholder='mail@mail.com' autoComplete='off' /> @@ -36,17 +110,23 @@ const SignUpForm = () => { className='rounded-2xl border border-[#d3d5e2] mt-1 w-full p-3 placeholder-[#9CA2A9] shadow-md text-sm' type='password' id='password' + name='password' + value={formData.password} + onChange={handleChange} placeholder='at least 8 characters' />
-
@@ -58,6 +138,9 @@ const SignUpForm = () => { className='rounded-2xl border border-[#d3d5e2] mt-1 w-full p-3 placeholder-[#9CA2A9] shadow-md text-sm' type='number' id='phone' + name='phone' + value={formData.phone} + onChange={handleChange} placeholder='+1122334455' autoComplete='off' /> @@ -68,12 +151,22 @@ const SignUpForm = () => { + > + + + + -