From 37184795fddc15273650e5439107379b5639bea8 Mon Sep 17 00:00:00 2001 From: sushant dhimal Date: Sat, 18 Mar 2023 22:32:30 +0545 Subject: [PATCH] add: add new user to firebase database - create a collection of user in firebase database when they signup --- client/src/App.css | 2 +- client/src/components/NavLink.jsx | 5 ++-- client/src/components/signup/SignUpForm.jsx | 27 ++++++++++++++++++--- client/src/firebase.config.js | 6 ++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/client/src/App.css b/client/src/App.css index 54e1dac..aa5173c 100644 --- a/client/src/App.css +++ b/client/src/App.css @@ -297,7 +297,7 @@ form button { top: 20%; right: 15px; width: 35px; - height: 35px; + height: 45px; } @media (hover: hover) { diff --git a/client/src/components/NavLink.jsx b/client/src/components/NavLink.jsx index c53eaf1..0093e02 100644 --- a/client/src/components/NavLink.jsx +++ b/client/src/components/NavLink.jsx @@ -2,6 +2,7 @@ import React, { useContext } from "react"; import { signOut } from "firebase/auth"; import { auth } from "../firebase.config"; import { AuthContext } from "../context/AuthContext"; +import { Link } from "react-router-dom"; const NavLinks = ({ svg, link, text, setChatLog }) => { const { dispatch } = useContext(AuthContext); @@ -20,7 +21,7 @@ const NavLinks = ({ svg, link, text, setChatLog }) => { }; return ( - { {svg}

{text}

-
+ ); }; diff --git a/client/src/components/signup/SignUpForm.jsx b/client/src/components/signup/SignUpForm.jsx index 3206d07..73a70d1 100644 --- a/client/src/components/signup/SignUpForm.jsx +++ b/client/src/components/signup/SignUpForm.jsx @@ -1,10 +1,11 @@ import React, { useContext, useState } from "react"; import "./signupform.css"; import { createUserWithEmailAndPassword, signInWithPopup } from "firebase/auth"; -import { auth, goggleAuthProvider } from "../../firebase.config"; +import { auth, db, goggleAuthProvider } from "../../firebase.config"; import { AuthContext } from "../../context/AuthContext"; import { useNavigate } from "react-router-dom"; import SvgComponent from "../SvgComponent"; +import { addDoc, collection } from "firebase/firestore"; const SignupForm = () => { const [email, setEmail] = useState(""); @@ -15,7 +16,7 @@ const SignupForm = () => { const { dispatch } = useContext(AuthContext); const navigate = useNavigate(); - const handleLogin = async (e) => { + const handleSignup = async (e) => { e.preventDefault(); try { @@ -26,6 +27,15 @@ const SignupForm = () => { ); const user = userCredential.user; dispatch({ type: "SIGNUP", payload: user }); + + // add user to firebase database + const docRef = await addDoc(collection(db, "users"), { + email, + password, + date_of_sign_up: new Date(), + }); + console.log("Document written with ID: ", docRef.id); + // once user is signed in navigate them to the home page navigate("/"); } catch (error) { @@ -42,6 +52,17 @@ const SignupForm = () => { const user = userCredential.user; dispatch({ type: "SIGNUP", payload: user }); console.log("user", user); + + // data when user signup with goggle account + const email = user.email; + const user_ID_from_Goggle_Sign_up = user.uid; + // add user to firebase database + const docRef = await addDoc(collection(db, "users"), { + email, + user_ID_from_Goggle_Sign_up, + date_of_sign_up: new Date(), + }); + console.log("Document written with ID: ", docRef.id); // once user is signed in navigate them to the home page navigate("/"); } catch (error) { @@ -56,7 +77,7 @@ const SignupForm = () => {

Create your account

-
+