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 1 commit
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.png
joriordan332 marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 24 additions & 15 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 @@ -43,22 +47,27 @@ 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} />}
/>
<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} />}
/>
<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
6 changes: 6 additions & 0 deletions src/utils/ProtectedRoutes.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Outlet, Navigate } from 'react-router-dom';

const ProtectedRoutes = ({ user }) => {
return user ? <Outlet /> : <Navigate to="/login" />; // Redirect to login if not authenticated
joriordan332 marked this conversation as resolved.
Show resolved Hide resolved
};
export default ProtectedRoutes;
30 changes: 30 additions & 0 deletions src/views/Login.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SignInButton, SignOutButton } from '../api/useAuth';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

function Login({ user }) {
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-lg h-2/4 w-5/12">
<div className="flex justify-center items-center h-2/4">
<img src="grocerease.png" alt="Shopping app logo" />
</div>
<div className="flex justify-center items-center h-2/4">
<div className="bg-pink-400 w-2/4 rounded text-center">
<SignInButton />
</div>
</div>
</div>
</div>
);
}

export default Login;
Loading