generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restrict Navigation Bar Links Based on User Authentication Status (#27)
* removed useAuth call in Layout to pass user info from app.tsx usage of useAuth * add user check on navbar to make sure only shows if log in, and update login buttons for user in or out * add ProtectedRoute to redirect navigation if not logged in to / * moved react-hot-toast Toaster to top level app to not have to use in each file toast is needed * updating signout button to redirect to home page on success & toast error to retry on error * added a PageNotFound componet with a catch all route incase manual nav to nonexistent page * removed unused import in useAuth * update useAuth to have useFindUser & useGetUser * updated protectedRoute to handle the User context needed in Outlet components * wrap all protected routes in one ProtectedRoute call * changes to short-circuit evaluations of the signin button and navbar in layout * moved navbar to own component * clean up the Toaster import on the files since it moved to the root * updated name of NavBar to reflect it is authenticated version * removing sign out button redirect since now handled in the protectedRoute * updated ShareListForm to utilize getUser in the ProtectedRoute hierarchy * update src/components index.ts to include ShareListForm
- Loading branch information
Showing
14 changed files
with
253 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./firebase"; | ||
export { useAuth } from "./useAuth"; | ||
export { useFindUser, SignInButton, SignOutButton } from "./useAuth"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.Nav { | ||
background-color: var(--color-bg); | ||
border-top: 1px solid var(--color-border); | ||
bottom: 0; | ||
display: flex; | ||
flex-direction: row; | ||
padding-bottom: max(env(safe-area-inset-bottom), 1rem); | ||
padding-top: 1rem; | ||
place-content: center; | ||
position: fixed; | ||
width: 100%; | ||
} | ||
|
||
.Nav-container { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-evenly; | ||
width: min(72ch, 100%); | ||
} | ||
|
||
.Nav-link { | ||
--color-text: var(--color-accent); | ||
color: var(--color-text); | ||
font-size: 1.4em; | ||
flex: 0 1 auto; | ||
line-height: 1; | ||
padding: 0.8rem; | ||
text-align: center; | ||
text-underline-offset: 0.1em; | ||
} | ||
|
||
.Nav-link.active { | ||
text-decoration-thickness: 0.22em; | ||
text-underline-offset: 0.1em; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from "react"; | ||
import { NavLink } from "react-router-dom"; | ||
import { SignOutButton } from "../api"; | ||
|
||
import "./AuthenticatedNavBar.css"; | ||
|
||
export function AuthenticatedNavBar() { | ||
return ( | ||
<nav className="Nav"> | ||
<div className="Nav-container"> | ||
<SignOutButton /> | ||
<NavLink to="/" className="Nav-link" aria-label="Home"> | ||
Home | ||
</NavLink> | ||
<NavLink to="/list" className="Nav-link" aria-label="List"> | ||
List | ||
</NavLink> | ||
<NavLink | ||
to="/manage-list" | ||
className="Nav-link" | ||
aria-label="Manage List" | ||
> | ||
Manage List | ||
</NavLink> | ||
</div> | ||
</nav> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from "react"; | ||
import { Navigate, Outlet, useOutletContext } from "react-router-dom"; | ||
import { User } from "../api"; | ||
|
||
interface Props { | ||
user: User | null; | ||
redirectPath: string; | ||
} | ||
|
||
type ProtectedRouteProps = { user: User }; | ||
|
||
export function ProtectRoute({ user, redirectPath }: Props) { | ||
return user ? ( | ||
<Outlet context={{ user } satisfies ProtectedRouteProps} /> | ||
) : ( | ||
<Navigate to={redirectPath} /> | ||
); | ||
} | ||
|
||
/** | ||
Gets the `user` object from the context of the `Outlet` component it is called in. | ||
The function has to be called within a nested component of a route protected by `ProtectRote`. | ||
It will allow user-specific information without the needs to pass `user` explicity through props. | ||
@returns {User} | ||
*/ | ||
export function getUser() { | ||
return useOutletContext<ProtectedRouteProps>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export * from "./ListItem"; | ||
export * from "./SingleList"; | ||
export * from "./CreateList"; | ||
export * from "./ProtectedRoute"; | ||
export * from "./AuthenticatedNavBar"; | ||
export * from "./forms/ShareListForm"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import React, { StrictMode } from "react"; | ||
import { createRoot } from "react-dom/client"; | ||
import { BrowserRouter as Router } from "react-router-dom"; | ||
import { App } from "./App"; | ||
|
||
import "./index.css"; | ||
|
||
const root = createRoot(document.getElementById("root") as HTMLElement); | ||
root.render( | ||
<StrictMode> | ||
<App /> | ||
<Router> | ||
<App /> | ||
</Router> | ||
</StrictMode>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.