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

Protect routes and create welcome page #39

Merged
merged 4 commits into from
Oct 6, 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
Binary file added public/grocerease-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/grocerease.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 26 additions & 17 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { useAuth, useShoppingListData, useShoppingLists } from './api';

import { useStateWithStorage } from './utils';

import ProtectedRoutes from './utils/ProtectedRoutes';

import Login from './views/Login';

export function App() {
/**
* This custom hook takes the path of a shopping list
Expand Down Expand Up @@ -45,24 +49,29 @@ export function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Layout />}>
<Route
index
element={
<Home user={user} data={lists} setListPath={setListPath} />
}
/>
<Route
path="/list"
element={
<List data={data} listPath={listPath} listName={listName} />
}
/>
<Route
path="/manage-list"
element={<ManageList listPath={listPath} user={user} data={data} />}
/>
<Route element={<ProtectedRoutes user={user} />}>
<Route path="/" element={<Layout />}>
<Route
index
element={
<Home user={user} data={lists} setListPath={setListPath} />
}
/>
<Route
path="/list"
element={
<List data={data} listPath={listPath} listName={listName} />
}
/>
<Route
path="/manage-list"
element={
<ManageList listPath={listPath} user={user} data={data} />
}
/>
</Route>
</Route>
<Route element={<Login user={user} />} path="/login" />
</Routes>
</Router>
);
Expand Down
25 changes: 21 additions & 4 deletions src/api/useAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,33 @@ export const SignOutButton = () => (
* @see https://firebase.google.com/docs/auth/web/start#set_an_authentication_state_observer_and_get_user_data
*/
export const useAuth = () => {
const [user, setUser] = useState(null);
const [user, setUser] = useState(() => {
const storedUser = localStorage.getItem('user');
return storedUser ? JSON.parse(storedUser) : null;
});

useEffect(() => {
auth.onAuthStateChanged((user) => {
setUser(user);
const unsubscribe = auth.onAuthStateChanged((user) => {
if (user) {
setUser(user);
localStorage.setItem('user', JSON.stringify(user));
addUserToDatabase(user);
} else {
setUser(null);
localStorage.removeItem('user');
}
});
return () => unsubscribe();
}, []);

return { user };
const signIn = async () => {
await signInWithPopup(auth, new GoogleAuthProvider());
};

const signOut = async () => {
await auth.signOut();
localStorage.removeItem('user');
};

return { user, signIn, signOut };
};
9 changes: 9 additions & 0 deletions src/utils/ProtectedRoutes.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Outlet, Navigate } from 'react-router-dom';
import { useAuth } from '../api/useAuth';

const ProtectedRoutes = ({}) => {
const { user } = useAuth();
const isAuthenticated = user || localStorage.getItem('user');
return isAuthenticated ? <Outlet /> : <Navigate to="/login" />; // Redirect to login if not authenticated
};
export default ProtectedRoutes;
33 changes: 33 additions & 0 deletions src/views/Login.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useAuth } from '../api/useAuth';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

function Login() {
const { user, signIn } = useAuth();
const navigate = useNavigate();

useEffect(() => {
if (user) {
navigate('/');
}
}, [user]);

return (
<div className="flex justify-center items-center h-screen">
joriordan332 marked this conversation as resolved.
Show resolved Hide resolved
<div className="bg-black text-white rounded-xl w-11/12 max-w-lg p-8 shadow-xl">
<div className="flex justify-center items-center mb-6">
<img src="grocerease-light.png" alt="Shopping app logo" />
</div>
<div className="flex justify-center items-center h-2/5">
<div className="flex justify-center items-center bg-pink-500 hover:bg-pink-500 hover:bg-opacity-80 w-2/4 h-1/4 rounded-xl p-2">
<button type="button" onClick={signIn}>
Sign In
</button>
</div>
</div>
</div>
</div>
);
}

export default Login;
Loading