diff --git a/.gitignore b/.gitignore index 5b062ef..29a20cb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ firebase.js # backend /backend/config.js /backend/node_modules -/backend/package-lock.json \ No newline at end of file +/backend/package-lock.json +.vercel diff --git a/student-match/src/app/loginForm/loginForm.tsx b/student-match/src/app/loginForm/loginForm.tsx index 8cff371..95f13ed 100644 --- a/student-match/src/app/loginForm/loginForm.tsx +++ b/student-match/src/app/loginForm/loginForm.tsx @@ -4,11 +4,7 @@ import React, { useState } from "react"; import "./loginForm.css"; import { useRouter } from "next/navigation"; // Correct import for Firebase v9+ -import { - getAuth, - createUserWithEmailAndPassword, - signInWithEmailAndPassword, -} from "firebase/auth"; +import { signInWithEmailAndPassword } from "firebase/auth"; import { auth } from "../firebase/firebase"; // Adjust the path as necessary const LoginForm: React.FC = () => { @@ -25,10 +21,42 @@ const LoginForm: React.FC = () => { setFormData({ ...formData, [name]: value }); }; + const getUserRequest = async (userId: string) => { + // Get user from MongoDB + const url = "http://ec2-3-140-189-217.us-east-2.compute.amazonaws.com/api/user/id/"; + const response = await fetch(url + userId, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + if (response.status == 200) { + const data = await response.json(); + // data is user data. Store in some state to indicate which user is currently logged in + global?.window?.localStorage?.setItem("user", JSON.stringify(data)); + return true; + } + return false; + }; + + // TODO: add warnings for invalid email/password, invalid auth, etc. const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); - global?.window?.localStorage?.setItem("user", JSON.stringify(formData)); - router.push("/homePage"); + signInWithEmailAndPassword(auth, formData.email, formData.password) + .then((res) => { + getUserRequest(res.user.uid) + .then((res) => { + if (res) { + router.push("/homePage"); + } + }) + .catch((error) => { + console.log("Error getting user:", error); + }); + }) + .catch((error) => { + console.log(error) + }); }; return ( diff --git a/student-match/src/app/signUpForm/page.tsx b/student-match/src/app/signUpForm/page.tsx index bd913e7..cc3f8fa 100644 --- a/student-match/src/app/signUpForm/page.tsx +++ b/student-match/src/app/signUpForm/page.tsx @@ -25,7 +25,7 @@ const SignUpForm: React.FC = () => { const createUserRequest = async (data: Object) => { // Adds user to MongoDB - const url = "http://localhost/api/user"; + const url = "http://ec2-3-140-189-217.us-east-2.compute.amazonaws.com/api/user"; const response = await fetch(url, { method: "POST", headers: { @@ -43,6 +43,7 @@ const SignUpForm: React.FC = () => { return false; }; + // TODO: add warnings for invalid email/password, if email exists, etc. const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); // Use Firebase Auth to create a new user @@ -54,12 +55,14 @@ const SignUpForm: React.FC = () => { email: formData.email, phoneNumber: formData.phoneNumber, }; - // call createUserRequest function with formData object createUserRequest(data) .then((res) => { if (res) { router.push("/homePage"); } + }) + .catch((error) => { + console.error("Error creating user:", error); }); }) .catch((error) => {