Skip to content

Commit

Permalink
feat: upgrades clerk version
Browse files Browse the repository at this point in the history
  • Loading branch information
ixahmedxi committed May 14, 2024
1 parent 6f9d232 commit 489aa18
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 49 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"db:migrate": "bun ./scripts/run-migrations.ts"
},
"dependencies": {
"@clerk/nextjs": "^4.27.7",
"@clerk/themes": "^1.7.9",
"@clerk/nextjs": "^5.0.9",
"@clerk/themes": "^2.1.4",
"@hookform/resolvers": "^3.3.4",
"@libsql/client": "^0.3.6",
"@mdx-js/loader": "^3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/app/(dashboard)/_components/side-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dashboardLinks, siteConfig } from "@/app/config";
import { Brand } from "@/components/brand";
import { currentUser } from "@clerk/nextjs";
import { currentUser } from "@clerk/nextjs/server";
import { Button } from "@nextui-org/button";
import { Link } from "@nextui-org/link";
import { BadgeHelpIcon } from "lucide-react";
Expand Down
2 changes: 1 addition & 1 deletion src/app/(dashboard)/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypographyH2 } from "@/components/TypographyH2";
import { TypographyP } from "@/components/TypographyP";
import { formatDate } from "@/utils/formatDate";
import { currentUser } from "@clerk/nextjs";
import { currentUser } from "@clerk/nextjs/server";
import { type Metadata } from "next";
import { RecentModules } from "../_components/recent-modules";
import { NotebookTable } from "./_components/notebook-table";
Expand Down
5 changes: 2 additions & 3 deletions src/app/(dashboard)/app/waitlist/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TypographyH2 } from "@/components/TypographyH2";
import { currentUser } from "@clerk/nextjs";
import type { User } from "@clerk/nextjs/api";
import { currentUser } from "@clerk/nextjs/server";
import { type Metadata } from "next";
import { Waitlist } from "../../_components/waitlist-table";

Expand All @@ -9,7 +8,7 @@ export const metadata: Metadata = {
};

export default async function WaitlistPage() {
const user: User | null = await currentUser();
const user = await currentUser();
const userMetadata = user?.publicMetadata;

if (userMetadata?.role === "ADMIN") {
Expand Down
54 changes: 13 additions & 41 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { authMiddleware } from "@clerk/nextjs";
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { NextResponse, type NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { env } from "./env.mjs";

let redis: Redis;
Expand All @@ -19,48 +19,20 @@ if (env.UPSTASH_REDIS_REST_URL) {
});
}

async function rateLimitMiddleware(
request: NextRequest,
): Promise<Response | undefined> {
const ip = request.ip ?? "127.0.0.1";
const { success } = await ratelimit.limit(ip);
return success
? NextResponse.next()
: NextResponse.redirect(new URL("/blocked", request.url));
}

const publicRoutesThatShouldRedirectAfterAuth = ["/", "/waitlist"];

export default authMiddleware({
beforeAuth: (req) => {
if (env.UPSTASH_REDIS_REST_URL) {
return rateLimitMiddleware(req);
}
return NextResponse.next();
},
afterAuth: (auth, req) => {
if (
auth.userId &&
publicRoutesThatShouldRedirectAfterAuth.includes(req.nextUrl.pathname)
) {
return NextResponse.redirect(new URL("/app", req.url));
}
const isProtectedRoute = createRouteMatcher(["/app(.*)"]);

if (!auth.userId && req.nextUrl.pathname.includes("/app")) {
return NextResponse.redirect(new URL("/sign-in", req.url));
}
export default clerkMiddleware(async (auth, req) => {
if (isProtectedRoute(req)) auth().protect();

return NextResponse.next();
},
publicRoutes: [
...publicRoutesThatShouldRedirectAfterAuth,
"/blocked",
"/editor",
"/privacy",
"/(api|trpc)(.*)",
],
if (env.UPSTASH_REDIS_REST_URL) {
const ip = req.ip ?? "127.0.0.1";
const { success } = await ratelimit.limit(ip);
return success
? NextResponse.next()
: NextResponse.redirect(new URL("/blocked", req.url));
}
});

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
matcher: ["/((?!.*\\..*|_next).*)", "/", "/blocked", "/(api|trpc)(.*)"],
};
2 changes: 1 addition & 1 deletion src/server/api/routers/waitlist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { insertWaitlist, waitlist } from "@/db";
import { clerkClient } from "@clerk/nextjs";
import { clerkClient } from "@clerk/nextjs/server";
import { LibsqlError } from "@libsql/client";
import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm";
Expand Down

0 comments on commit 489aa18

Please sign in to comment.