diff --git a/compass/app/home/layout.tsx b/compass/app/home/layout.tsx index b98493c..236616e 100644 --- a/compass/app/home/layout.tsx +++ b/compass/app/home/layout.tsx @@ -15,7 +15,6 @@ export default function RootLayout({ const [isSidebarOpen, setIsSidebarOpen] = useState(true); const [user, setUser] = useState(); const router = useRouter(); - const [loading, setLoading] = useState(true); useEffect(() => { async function getUser() { @@ -36,7 +35,6 @@ export default function RootLayout({ ); setUser(await userData.json()); - setLoading(false); } getUser(); @@ -52,7 +50,6 @@ export default function RootLayout({ setIsSidebarOpen={setIsSidebarOpen} isSidebarOpen={isSidebarOpen} isAdmin={user.role === Role.ADMIN} - loading={loading} />
= ({ @@ -28,7 +26,6 @@ const Sidebar: React.FC = ({ name, email, isAdmin: admin, - loading, }) => { const [isLoading, setIsLoading] = useState(false); return ( diff --git a/compass/components/Sidebar/SidebarItem.tsx b/compass/components/Sidebar/SidebarItem.tsx index 475f7b5..6a94d91 100644 --- a/compass/components/Sidebar/SidebarItem.tsx +++ b/compass/components/Sidebar/SidebarItem.tsx @@ -1,4 +1,5 @@ import Link from "next/link"; +import { usePathname } from "next/navigation"; interface SidebarItemProps { icon: React.ReactElement; @@ -15,9 +16,13 @@ export const SidebarItem: React.FC = ({ redirect, onClick, }) => { + const pathname = usePathname(); + return ( onClick(true)} + onClick={() => + pathname.startsWith(redirect) ? onClick(false) : onClick(true) + } href={redirect} className={ active diff --git a/compass/components/resource/SearchResult.tsx b/compass/components/resource/SearchResult.tsx index 42b1b9e..3e4f3f6 100644 --- a/compass/components/resource/SearchResult.tsx +++ b/compass/components/resource/SearchResult.tsx @@ -23,8 +23,8 @@ export const SearchResult: React.FC = ({ type === "resource" ? BookmarkIcon : type === "service" - ? ClipboardIcon - : QuestionMarkCircleIcon; // Unknown type + ? ClipboardIcon + : QuestionMarkCircleIcon; // Unknown type return (
diff --git a/compass/components/resource/sample_results.json b/compass/components/resource/sample_results.json index 275250c..f8b9df6 100644 --- a/compass/components/resource/sample_results.json +++ b/compass/components/resource/sample_results.json @@ -1,32 +1,32 @@ [ - { - "type": "resource", - "name": "example name", - "description": "example description" - }, - { - "type": "service", - "name": "example name", - "description": "example description" - }, - { - "type": "resource", - "name": "National Domestic Violence Hotline", - "description": "24/7 confidential support for victims of domestic violence" - }, - { - "type": "resource", - "name": "Legal Aid Society", - "description": "Free legal assistance for low-income individuals" - }, - { - "type": "service", - "name": "Crisis Hotline", - "description": "24/7 support for individuals in crisis" - }, - { - "type": "unknown", - "name": "unknown thing with a really long name", - "description": "and let's also type out a really long description to see how it handles overflow and all that anyways" - } + { + "type": "resource", + "name": "example name", + "description": "example description" + }, + { + "type": "service", + "name": "example name", + "description": "example description" + }, + { + "type": "resource", + "name": "National Domestic Violence Hotline", + "description": "24/7 confidential support for victims of domestic violence" + }, + { + "type": "resource", + "name": "Legal Aid Society", + "description": "Free legal assistance for low-income individuals" + }, + { + "type": "service", + "name": "Crisis Hotline", + "description": "24/7 support for individuals in crisis" + }, + { + "type": "unknown", + "name": "unknown thing with a really long name", + "description": "and let's also type out a really long description to see how it handles overflow and all that anyways" + } ] diff --git a/compass/utils/supabase/middleware.ts b/compass/utils/supabase/middleware.ts index 13fc50a..101153e 100644 --- a/compass/utils/supabase/middleware.ts +++ b/compass/utils/supabase/middleware.ts @@ -1,5 +1,7 @@ import { createServerClient, type CookieOptions } from "@supabase/ssr"; -import { NextResponse, type NextRequest } from "next/server"; +import { User } from "@supabase/supabase-js"; +import { NextRequest, NextResponse } from "next/server"; +import { Role } from "../models/User"; export async function updateSession(request: NextRequest) { let response = NextResponse.next({ @@ -54,7 +56,50 @@ export async function updateSession(request: NextRequest) { } ); - await supabase.auth.getUser(); + const { data, error } = await supabase.auth.getUser(); + + const authenticatedRoutes = ["/admin", "/resource", "/home", "/service"]; + const pathname = request.nextUrl.pathname; + + for (const route of authenticatedRoutes) { + if (error && pathname.startsWith(route)) { + console.log("redirected"); + return NextResponse.redirect( + new URL( + "/auth/login", + request.nextUrl.protocol + "//" + request.nextUrl.host + ) + ); + } + } + + if (pathname.startsWith("/admin") && data.user) { + // After the previous checks we can assume the user is not empty + const userData = await fetch( + `${process.env.NEXT_PUBLIC_HOST}/api/user?uuid=${data.user.id}` + ); + + const user: User = await userData.json(); + + if (user.role !== Role.ADMIN) { + console.log("redirected as not admin"); + return NextResponse.redirect( + new URL( + "/home", + request.nextUrl.protocol + "//" + request.nextUrl.host + ) + ); + } + } + + if (data.user && pathname.startsWith("/auth/login")) { + return NextResponse.redirect( + new URL( + "/home", + request.nextUrl.protocol + "//" + request.nextUrl.host + ) + ); + } return response; }