diff --git a/src/components/login.jsx b/src/components/login.jsx index 45217f9..a07697c 100644 --- a/src/components/login.jsx +++ b/src/components/login.jsx @@ -1,52 +1,19 @@ import React, { useState } from 'react'; import '../App.css'; -import mainImg from '../assets/codeit-logo.png' -// import './bg-main.css' -import '../styles/signup.css' -import { login } from '../utilities/Login' +import mainImg from '../assets/codeit-logo.png'; +import '../styles/signup.css'; +import { signInWithEmailAndPassword, signInWithPopup } from "firebase/auth"; import { useNavigate } from 'react-router-dom'; -import { useAuthValue } from '../utilities/AuthContext' -import { auth, db } from "../utilities/firebase"; -import { signInWithEmailAndPassword } from "firebase/auth"; -import { redirect } from 'react-router-dom' -import { writeBatch, doc, addDoc, collection } from 'firebase/firestore'; +import { useAuthValue } from '../utilities/AuthContext'; +import { auth, provider } from '../firebaseConfig' export default function Login() { - - // async function importJSONToFirestore(collectionName, jsonArray) { - // try { - // const batch = writeBatch(db); - // jsonArray.forEach((jsonObject) => { - // // Generate a new document reference for each JSON object - // addDoc(collection(db, collectionName), jsonObject); - // // batch.set(newDocRef, jsonObject); - // }); - - // // Commit the batch - // // await batch.commit(); - - // // console.log('Import completed successfully.'); - // } catch (error) { - // console.error('Error importing data to Firestore:', error); - // } - // } - - // Example usage - - - - - const collectionName = 'problems'; // Replace with your desired collection name - - - const [loginData, setLoginData] = useState({ email: '', password: '', }); const navigate = useNavigate(); - const { currentUser } = useAuthValue() - // // console.log(loginData.email) + const { currentUser } = useAuthValue(); const handleInputChange = (event) => { const { name, value } = event.target; @@ -56,40 +23,25 @@ export default function Login() { })); }; - const login = async (email, password, e) => { - // e.preventDefault(); - // setError(""); - // debugger + const login = async (email, password) => { signInWithEmailAndPassword(auth, email, password) - .then((res) => { - // debugger - // console.log(res.user); - // debugger - navigate('/dashboard') + .then(() => { + navigate('/dashboard'); }) .catch((err) => alert(err.message)); - - // setEmail(""); - // setPassword(""); - // setConfirmPassword(""); }; - // const handleSubmit = async (event) => { - // event.preventDefault(); - // const { email, password } = loginData; - - - // const params = new URLSearchParams(); - // params.append('email', email); - // params.append('password', password); - - // const url = `register?${params}`; - - // let postReq = await fetch(url) - // // console.log("hiiii") - // let res = await postReq.json() - // // console.log('Logged in with', res.body); - // }; + const loginWithGoogle = async () => { + signInWithPopup(auth, provider) + .then((result) => { + // User logged in with Google + navigate('/dashboard'); + }) + .catch((error) => { + console.error(error); + alert('Google sign-in failed!'); + }); + }; return (
@@ -129,18 +81,14 @@ export default function Login() { />
-
- -
+

or

-
-

Create your account

+

Create your account

- {/* - */} -
-
- +

or

- @@ -188,4 +144,4 @@ export default function Signup() {
); -} \ No newline at end of file +} diff --git a/src/firebaseConfig.js b/src/firebaseConfig.js index 1668014..6e79f16 100644 --- a/src/firebaseConfig.js +++ b/src/firebaseConfig.js @@ -1,10 +1,8 @@ -// Import the functions you need from the SDKs you need import { initializeApp } from "firebase/app"; +import { getAuth, GoogleAuthProvider, signInWithPopup } from "firebase/auth"; import { getFirestore } from "firebase/firestore"; -// TODO: Add SDKs for Firebase products that you want to use -// https://firebase.google.com/docs/web/setup#available-libraries -// Your web app's Firebase configuration +// Firebase config (you already have this in your project) const firebaseConfig = { apiKey: import.meta.env.VITE_APP_API_KEY, authDomain: import.meta.env.VITE_APP_AUTH_DOMAIN, @@ -15,4 +13,9 @@ const firebaseConfig = { }; // Initialize Firebase -const app = initializeApp(firebaseConfig); \ No newline at end of file +const app = initializeApp(firebaseConfig); +const auth = getAuth(app); +const db = getFirestore(app); +const provider = new GoogleAuthProvider(); + +export { auth, db, provider };