Skip to content

Commit

Permalink
user display name parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
NishilJ committed Nov 20, 2024
1 parent 7968d8c commit 0a52df1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 68 deletions.
10 changes: 3 additions & 7 deletions src/components/GoogleSSO.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ 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);
}
};

return (
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
<Button
fullWidth
variant="outlined"
onClick={handleGoogleSignIn}
startIcon={<GoogleIcon />}
>
<Button fullWidth variant="outlined" onClick={handleGoogleSignIn} startIcon={<GoogleIcon />}>
Sign in with Google
</Button>
</Box>
Expand Down
20 changes: 8 additions & 12 deletions src/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,18 +39,11 @@ const Login: React.FC = () => {
<Container maxWidth="xs" sx={{ mt: 4 }}>
<Stack spacing={2} alignItems="center">
<Typography variant="h5">Login</Typography>
<TextField
label="Email"
variant="outlined"
fullWidth
<TextField label="Email" variant="outlined" fullWidth
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<TextField
label="Password"
type="password"
variant="outlined"
fullWidth
<TextField label="Password" type="password" variant="outlined" fullWidth
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
Expand Down
67 changes: 37 additions & 30 deletions src/components/Register.tsx
Original file line number Diff line number Diff line change
@@ -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<string>('');
const [email, setEmail] = useState<string>('');
const [password, setPassword] = useState<string>('');
const [confirmPassword, setConfirmPassword] = useState<string>('');
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 (
<Container maxWidth="xs" sx={{ mt: 4 }}>
<Stack spacing={2} alignItems="center">
<Typography variant="h5">Register</Typography>
<TextField
label="Email"
variant="outlined"
fullWidth
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<TextField
label="Password"
type="password"
variant="outlined"
fullWidth
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<Button variant="contained" color="primary" onClick={handleRegister} fullWidth>
Register
</Button>
</Stack>
</Container>
);
return (
<Container maxWidth="xs" sx={{ mt: 4 }}>
<Stack spacing={2} alignItems="center">
<Typography variant="h5">Register</Typography>
<TextField label="Name" variant="outlined" fullWidth value={name}
onChange={(e) => setName(e.target.value)}
/>
<TextField label="Email" variant="outlined" fullWidth value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<TextField label="Password" type="password" variant="outlined" fullWidth
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<TextField label="Confirm Password" type="password" variant="outlined" fullWidth
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
/>
<Button variant="contained" color="primary" onClick={handleRegister} fullWidth>
Register
</Button>
</Stack>
</Container>
);
};

export default Register;
export {};
53 changes: 34 additions & 19 deletions src/components/TopNavBar.tsx
Original file line number Diff line number Diff line change
@@ -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<string | null>(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 (
<Flex margin="auto" justifyContent="space-between" alignItems="center" width="95%" height="100%">
<Heading level={1}>Metroll</Heading>
<MenuTrigger>
<Flex alignItems="center" gap="size-200">
{isLoggedIn && userName && <Heading level={4}>Welcome, {userName}</Heading>}
<MenuTrigger>
<ActionButton></ActionButton>
<Menu onAction={handleMenuOptions}>
{!isLoggedIn ? (
<Item href="/login" key="login">Login/Sign In</Item>
<Item href="/login" key="login">Login/Sign In</Item>
) : (
<>
<Item href="/account" key="account">Account</Item>
<Item key="signout">Sign Out</Item>
</>
<>
<Item href="/account" key="account">Account</Item>
<Item key="signout">Sign Out</Item>
</>
)}
<Item href="/" key="home">Home</Item>
<Item href="/about" key="about">About</Item>
</Menu>
</MenuTrigger>
</MenuTrigger>
</Flex>
</Flex>
);
};
Expand Down

0 comments on commit 0a52df1

Please sign in to comment.