-
-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
36 additions
and
39 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { SignOutButton } from '@clerk/nextjs'; | ||
|
||
/** | ||
* This is the main page for the dashboard. | ||
* | ||
* @returns A Next.js RSC page component. | ||
*/ | ||
export default function DashboardHome() { | ||
return ( | ||
<main> | ||
<h1>Hello World</h1> | ||
<SignOutButton /> | ||
</main> | ||
); | ||
} |
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
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,8 @@ | ||
import { Redis } from '@upstash/redis'; | ||
|
||
import { env } from '@/env'; | ||
|
||
export const redis = new Redis({ | ||
url: env.UPSTASH_REDIS_REST_URL, | ||
token: env.UPSTASH_REDIS_REST_TOKEN, | ||
}); |
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,42 +1,11 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'; | ||
import { Ratelimit } from '@upstash/ratelimit'; | ||
import { Redis } from '@upstash/redis'; | ||
|
||
import { env } from './env'; | ||
|
||
let redis: Redis; | ||
let ratelimit: Ratelimit; | ||
|
||
if (env.UPSTASH_REDIS_REST_URL) { | ||
redis = new Redis({ | ||
url: env.UPSTASH_REDIS_REST_URL ?? '', | ||
token: env.UPSTASH_REDIS_REST_TOKEN ?? '', | ||
}); | ||
|
||
ratelimit = new Ratelimit({ | ||
redis: redis, | ||
limiter: Ratelimit.slidingWindow(20, '3 s'), | ||
}); | ||
} | ||
|
||
const isProtectedRoute = createRouteMatcher(['/app(.*)']); | ||
|
||
export default clerkMiddleware(async (auth, req) => { | ||
export default clerkMiddleware((auth, req) => { | ||
if (isProtectedRoute(req)) auth().protect(); | ||
|
||
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)); | ||
} | ||
|
||
return NextResponse.next(); | ||
}); | ||
|
||
export const config = { | ||
matcher: ['/((?!.*\\..*|_next).*)', '/', '/blocked', '/(api|trpc)(.*)'], | ||
matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'], | ||
}; |