diff --git a/apps/web/src/app/admin/layout.tsx b/apps/web/src/app/admin/layout.tsx index 7f27b77d..eb215a1c 100644 --- a/apps/web/src/app/admin/layout.tsx +++ b/apps/web/src/app/admin/layout.tsx @@ -8,6 +8,7 @@ import Navbar from "@/components/shared/navbar"; import DashNavItem from "@/components/dash/shared/DashNavItem"; import ClientToast from "@/components/shared/client-toast"; import c from "config"; +import { ADMIN_ROLES } from "@/lib/constants"; export default async function AdminLayout({ children, @@ -24,7 +25,11 @@ export default async function AdminLayout({ where: eq(users.clerkID, userId), }); - if (!user || (user.role !== "admin" && user.role !== "super_admin")) { + if (!user) { + return redirect("/onboarding"); + } + + if (!ADMIN_ROLES.includes(user.role)) { console.log("Denying admin access to user", user); return ( -
+

ClubKit

+ +

Find Events →

+
); diff --git a/apps/web/src/components/shared/navbar.tsx b/apps/web/src/components/shared/navbar.tsx index 29a3ab7d..272fdeba 100644 --- a/apps/web/src/components/shared/navbar.tsx +++ b/apps/web/src/components/shared/navbar.tsx @@ -14,9 +14,10 @@ import { SheetTitle, SheetTrigger, } from "@/components/ui/sheet"; - +import { ADMIN_ROLES } from "@/lib/constants"; import c from "config"; import { Menu } from "lucide-react"; +import { SignOutButton } from "@clerk/nextjs"; type NavbarProps = { siteRegion?: string; @@ -33,8 +34,8 @@ export default async function Navbar({ siteRegion, showBorder }: NavbarProps) { with: { data: true }, }) : null; - - const registrationComplete = user != null; + const hasSignedIn = userId != null; + const hasCompletedRegistration = user != null; return (
- {user ? ( + {hasSignedIn ? ( <> - - - - {(user.role === "admin" || - user.role === "super_admin") && ( - - - - )} + + {hasCompletedRegistration && + ADMIN_ROLES.includes(user.role) && ( + + + + )} + - {user ? ( + {hasSignedIn ? ( <> - {registrationComplete && ( + {hasCompletedRegistration && ( - - - - {(user.role === "admin" || - user.role === "super_admin") && ( - - - - )} + + {hasCompletedRegistration && + ADMIN_ROLES.includes(user.role) && ( + + + + )}
- Report a Bug diff --git a/apps/web/src/lib/constants/index.ts b/apps/web/src/lib/constants/index.ts index f13fe6f8..89207e10 100644 --- a/apps/web/src/lib/constants/index.ts +++ b/apps/web/src/lib/constants/index.ts @@ -3,3 +3,5 @@ export const TWENTY_FOUR_HOURS = 24; export const UNIQUE_KEY_CONSTRAINT_VIOLATION_CODE = "23505"; export const LOWER_ALPHANUM_CUSTOM_ALPHABET = "1234567890abcdefghijklmnopqrstuvwxyz"; + +export const ADMIN_ROLES: Array = ["admin", "super_admin"] as const; diff --git a/apps/web/src/lib/queries/users.ts b/apps/web/src/lib/queries/users.ts index 563141d2..6311dd89 100644 --- a/apps/web/src/lib/queries/users.ts +++ b/apps/web/src/lib/queries/users.ts @@ -2,13 +2,16 @@ import c from "config"; import { count, db, eq, sum } from "db"; import { checkins, data, events, users } from "db/schema"; import { getCurrentSemester } from "./semesters"; +import { ADMIN_ROLES } from "../constants"; + +type UserRoles = (typeof users.$inferSelect.role)[]; export const getAdminUser = async (clerkId: string) => { return db.query.users.findFirst({ where: (users, { eq, and, inArray }) => and( eq(users.clerkID, clerkId), - inArray(users.role, ["admin", "super_admin"]), + inArray(users.role, ADMIN_ROLES as UserRoles), ), }); }; diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts index cc6ace05..1dd36797 100644 --- a/apps/web/src/middleware.ts +++ b/apps/web/src/middleware.ts @@ -1,6 +1,7 @@ import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server"; import { getAdminUser } from "./lib/queries/users"; import { NextResponse } from "next/server"; + const isProtectedRoute = createRouteMatcher([ "/dash(.*)", "/admin(.*)", @@ -8,12 +9,12 @@ const isProtectedRoute = createRouteMatcher([ ]); const isAdminAPIRoute = createRouteMatcher(["/api/admin(.*)"]); -// come back and check if this is valid export default clerkMiddleware(async (auth, req) => { - const { userId } = await auth(); - - if (isProtectedRoute(req)) { - await auth.protect(); + const { userId, redirectToSignIn } = await auth(); + if (isProtectedRoute(req) && !userId) { + redirectToSignIn({ + returnBackUrl: req.nextUrl.toString(), + }); } // protect admin api routes