Skip to content

Commit

Permalink
Merge pull request #9 from mpak123/finished-auth
Browse files Browse the repository at this point in the history
finished login/signup capability with firebase auth
  • Loading branch information
mpak123 authored Apr 8, 2024
2 parents 83c45d1 + 5e0706d commit ded2028
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ firebase.js
# backend
/backend/config.js
/backend/node_modules
/backend/package-lock.json
/backend/package-lock.json
.vercel
42 changes: 35 additions & 7 deletions student-match/src/app/loginForm/loginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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<HTMLFormElement>) => {
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 (
Expand Down
7 changes: 5 additions & 2 deletions student-match/src/app/signUpForm/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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
Expand All @@ -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) => {
Expand Down

0 comments on commit ded2028

Please sign in to comment.