Skip to content
Open
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
13 changes: 11 additions & 2 deletions apps/web/src/app/sign-in/[[...sign-in]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// "use client"
import { SignIn } from "@clerk/nextjs";
"use client";
import { SignIn, useAuth } from "@clerk/nextjs";
import { Button } from "@/components/ui/button";
import c from "config";
import Link from "next/link";
import PortalMigrationExplainer from "@/components/dash/shared/PortalMigrationExplainer";

export default function Page() {
const {isLoaded } = useAuth();

if (!isLoaded) {
return (
<main className="flex h-screen w-screen items-center justify-center">
</main>
);
}
return (
<main className="flex h-screen w-screen flex-col items-center justify-center gap-y-5">
<div className="flex max-w-[400px] flex-col items-center justify-center gap-y-5">
Expand Down
12 changes: 11 additions & 1 deletion apps/web/src/app/sign-up/[[...sign-up]]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { SignUp } from "@clerk/nextjs";
"use client";
import { SignUp, useAuth } from "@clerk/nextjs";
import { Button } from "@/components/ui/button";
import c from "config";
import Link from "next/link";
import PortalMigrationExplainer from "@/components/dash/shared/PortalMigrationExplainer";
export default function Page() {
const {isLoaded } = useAuth();

if (!isLoaded) {
return (
<main className="flex h-screen w-screen items-center justify-center">
</main>
);
}
return (

<main className="flex h-screen w-screen flex-col items-center justify-center gap-y-5">
<div className="flex max-w-[400px] flex-col items-center justify-center gap-y-5">
<h1 className="text-4xl font-black">ClubKit</h1>
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/lib/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export const getAdminUser = async (clerkId: string) => {
});
};

export const getUserByClerkId = async (clerkId: string) => {
return db.query.users.findFirst({
where: eq(users.clerkID, clerkId),
});
};

export const getUser = async (userID: string) => {
return db.query.users.findFirst({
where: eq(users.userID, Number(userID)),
Expand Down
30 changes: 27 additions & 3 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { getAdminUser } from "./lib/queries/users";
import { getAdminUser, getUserByClerkId } from "./lib/queries/users";
import { NextResponse } from "next/server";

const isProtectedRoute = createRouteMatcher([
Expand All @@ -8,16 +8,40 @@ const isProtectedRoute = createRouteMatcher([
"/settings(.*)",
]);
const isAdminAPIRoute = createRouteMatcher(["/api/admin(.*)"]);
const isAuthRoute = createRouteMatcher(["/sign-in(.*)", "/sign-up(.*)"]);
const isOnboardingRoute = createRouteMatcher(["/onboarding(.*)"]);

export default clerkMiddleware(async (auth, req) => {
const { userId, redirectToSignIn } = await auth();

// Protect routes - redirect to sign-in if not authenticated
if (isProtectedRoute(req) && !userId) {
redirectToSignIn({
return redirectToSignIn({
returnBackUrl: req.nextUrl.toString(),
});
}

// protect admin api routes
// Handle authenticated user routing
if (userId) {
const user = await getUserByClerkId(userId);

// Redirect authenticated users away from auth pages
if (isAuthRoute(req)) {
return NextResponse.redirect(new URL(user ? "/dash" : "/onboarding", req.url));
}

// Redirect registered users away from onboarding
if (isOnboardingRoute(req) && user) {
return NextResponse.redirect(new URL("/dash", req.url));
}

// Redirect unregistered users to onboarding from protected routes
if (isProtectedRoute(req) && !user) {
return NextResponse.redirect(new URL("/onboarding", req.url));
}
}

// Protect admin API routes
if (isAdminAPIRoute(req)) {
if (!userId || !(await getAdminUser(userId))) {
return NextResponse.json({ error: "Unauthorized", status: 401 });
Expand Down
Loading