import React, { useState } from 'react';
import { signIn } from './AuthService';
import { useHistory } from 'react-router-dom';
function SignIn() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const history = useHistory();
const handleSubmit = async (e) => {
e.preventDefault();
try {
await signIn(email, password);
// Redirect to the home page or dashboard after successful sign-in
history.push('/');
} catch (error) {
// Handle sign-in errors here, such as incorrect credentials
alert(error.message);
}
};
return (
<div>
<h1>Sign In</h1>
<form onSubmit={handleSubmit}>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Email"
required
/>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
required
/>
<button type="submit">Sign In</button>
</form>
</div>
);
}
export default SignIn;
import React, { useEffect, useState } from 'react';
import firebase from './firebase';
import { signOut } from './AuthService';
import { useHistory } from 'react-router-dom';
function Home() {
const [currentUser, setCurrentUser] = useState(null);
const history = useHistory();
useEffect(() => {
// Subscribe to auth state changes
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
if (user) {
// User is signed in
setCurrentUser(user);
} else {
// No user is signed in, redirect to sign-in page
history.push('/signin');
}
});
// Cleanup subscription on unmount
return () => unsubscribe();
}, [history]);
const handleSignOut = async () => {
try {
await signOut();
history.push('/signin');
} catch (error) {
// Handle errors here - maybe show a notification or message
console.error('Sign out failed', error);
alert('Sign out failed: ' + error.message);
}
};
return (
<div>
<h1>Home</h1>
{currentUser && <p>Welcome, {currentUser.email}!</p>}
<button onClick={handleSignOut}>Sign Out</button>
</div>
);
}
export default Home;
import firebase from './firebase';
export const signOut = () => {
return firebase.auth().signOut();
};
import React, { useEffect, useState } from 'react';
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import firebase from './firebase';
import SignUp from './SignUp'; // Import your SignUp component
import SignIn from './SignIn'; // Import your SignIn component
import Home from './Home'; // Import your Home component
function App() {
const [currentUser, setCurrentUser] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
// Listen for auth state changes
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
setCurrentUser(user);
setLoading(false);
});
// Clean up the listener on unmount
return unsubscribe;
}, []);
if (loading) {
// Possibly include some loading indicator while waiting for auth state
return <div>Loading...</div>;
}
return (
<Router>
<Switch>
<Route exact path="/" render={() => (
currentUser ? <Home /> : <Redirect to="/signin" />
)} />
<Route path="/signup" render={() => (
!currentUser ? <SignUp /> : <Redirect to="/" />
)} />
<Route path="/signin" render={() => (
!currentUser ? <SignIn /> : <Redirect to="/" />
)} />
{/* Redirect all other paths to the home page, update as needed */}
<Route render={() => <Redirect to="/" />} />
</Switch>
</Router>
);
}
export default App;