Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished login/signup capability with firebase auth #9

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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