From 0a52df1c748680c1a1209dd4ff38b020aa206f8d Mon Sep 17 00:00:00 2001
From: NishilJ <78994772+NishilJ@users.noreply.github.com>
Date: Wed, 20 Nov 2024 12:24:49 -0600
Subject: [PATCH] user display name parameter
---
src/components/GoogleSSO.tsx | 10 ++----
src/components/Login.tsx | 20 +++++------
src/components/Register.tsx | 67 ++++++++++++++++++++----------------
src/components/TopNavBar.tsx | 53 ++++++++++++++++++----------
4 files changed, 82 insertions(+), 68 deletions(-)
diff --git a/src/components/GoogleSSO.tsx b/src/components/GoogleSSO.tsx
index 6eca5e0..3fb3da6 100644
--- a/src/components/GoogleSSO.tsx
+++ b/src/components/GoogleSSO.tsx
@@ -15,7 +15,8 @@ const GoogleSSO: React.FC = () => {
await signInWithPopup(auth, googleProvider);
alert('Google Sign-In Successful');
navigate('/'); // Redirect to homepage
- } catch (error) {
+ }
+ catch (error) {
console.error("Error with Google Sign-In:", error);
alert((error as Error).message);
}
@@ -23,12 +24,7 @@ const GoogleSSO: React.FC = () => {
return (
- }
- >
+ }>
Sign in with Google
diff --git a/src/components/Login.tsx b/src/components/Login.tsx
index 6e97ca0..f93be93 100644
--- a/src/components/Login.tsx
+++ b/src/components/Login.tsx
@@ -13,13 +13,16 @@ const Login: React.FC = () => {
useEffect(() => {
// Redirect if user is already logged in
- const unsubscribe = onAuthStateChanged(auth, (user) => {
+ const isLoggedIn = onAuthStateChanged(auth, (user) => {
if (user) {
navigate('/'); // Redirect to homepage if authenticated
}
});
- return () => unsubscribe(); // Cleanup listener on component unmount
- }, [navigate]);
+
+ return () => isLoggedIn(); // Cleanup listener on component unmount
+ },
+ [navigate]
+ );
const handleLogin = async () => {
try {
@@ -36,18 +39,11 @@ const Login: React.FC = () => {
Login
- setEmail(e.target.value)}
/>
- setPassword(e.target.value)}
/>
diff --git a/src/components/Register.tsx b/src/components/Register.tsx
index 3697247..5652882 100644
--- a/src/components/Register.tsx
+++ b/src/components/Register.tsx
@@ -1,52 +1,59 @@
-// src/Register.tsx
import React, { useState } from 'react';
-import { createUserWithEmailAndPassword } from 'firebase/auth';
+import { createUserWithEmailAndPassword, updateProfile } from 'firebase/auth';
import { auth } from '../firebaseconfig';
import { useNavigate } from 'react-router-dom';
import { Button, Container, Stack, TextField, Typography } from '@mui/material';
// Justin & Syed
const Register: React.FC = () => {
+ const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
+ const [confirmPassword, setConfirmPassword] = useState('');
const navigate = useNavigate();
+
const handleRegister = async () => {
+ if (password !== confirmPassword) {
+ alert('Passwords do not match');
+ return;
+ }
+
try {
- await createUserWithEmailAndPassword(auth, email, password);
+ const userCredential = await createUserWithEmailAndPassword(auth, email, password);
+ await updateProfile(userCredential.user, { displayName: name });
alert('Registration Successful');
navigate('/login');
- } catch (error) {
+ }
+ catch (error) {
console.error("Error registering:", error);
alert((error as Error).message);
}
};
- return (
-
-
- Register
- setEmail(e.target.value)}
- />
- setPassword(e.target.value)}
- />
-
-
-
- );
+ return (
+
+
+ Register
+ setName(e.target.value)}
+ />
+ setEmail(e.target.value)}
+ />
+ setPassword(e.target.value)}
+ />
+ setConfirmPassword(e.target.value)}
+ />
+
+
+
+ );
};
export default Register;
-export {};
diff --git a/src/components/TopNavBar.tsx b/src/components/TopNavBar.tsx
index 123f7a8..39c70e7 100644
--- a/src/components/TopNavBar.tsx
+++ b/src/components/TopNavBar.tsx
@@ -1,54 +1,69 @@
import React, { useEffect, useState } from 'react';
-import { Flex, Heading, MenuTrigger, ActionButton, Menu, Item } from '@adobe/react-spectrum';
+import { Flex, Heading, MenuTrigger, ActionButton, Menu, Item} from '@adobe/react-spectrum';
import { getAuth, onAuthStateChanged, signOut } from 'firebase/auth';
import { Key } from 'react';
// Nishil & Syed
// Implements the top navigation bar
const TopNavBar: React.FC = () => {
- const [isLoggedIn, setIsLoggedIn] = useState(false);
- const auth = getAuth();
+ const [isLoggedIn, setIsLoggedIn] = useState(false);
+ const [userName, setUserName] = useState(null);
+ const auth = getAuth();
useEffect(() => {
- const unsubscribe = onAuthStateChanged(auth, (user) => {
- setIsLoggedIn(!!user);
+ const isLoggedIn = onAuthStateChanged(auth, (user) => {
+ if (user) {
+ setIsLoggedIn(true);
+ setUserName(user.displayName); // Set display name from Firebase
+ } else {
+ setIsLoggedIn(false);
+ setUserName(null);
+ }
});
- return () => unsubscribe();
- }, [auth]);
+
+ return () => isLoggedIn();
+ },
+ [auth]
+ );
const handleSignOut = async () => {
try {
- await signOut(auth);
- setIsLoggedIn(false);
- } catch (error) {
- console.error("Sign out failed", error);
+ await signOut(auth);
+ setIsLoggedIn(false);
+ setUserName(null);
+ }
+ catch (error) {
+ console.error("Sign out failed", error);
}
};
const handleMenuOptions = (key: Key) => {
if (key === 'signout') {
- handleSignOut();
+ handleSignOut();
}
};
return (
Metroll
-
+
+ {isLoggedIn && userName && Welcome, {userName}}
+
☰
-
+
+
);
};