Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add clerk integration in /next branch #364

Merged
merged 8 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ words:
- typecheck
- Uploadthing
- Upstash
- ratelimit
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@
}
},
"dependencies": {
"@clerk/nextjs": "^5.0.12",
"@clerk/themes": "^2.1.6",
"@radix-ui/colors": "^3.0.0",
"@t3-oss/env-nextjs": "^0.10.1",
"@upstash/ratelimit": "^1.1.3",
"@upstash/redis": "^1.31.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"geist": "^1.3.0",
Expand Down
26 changes: 16 additions & 10 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Metadata } from 'next';

import { ClerkProvider } from '@clerk/nextjs';
import { dark } from '@clerk/themes';
import { GeistMono } from 'geist/font/mono';
import { GeistSans } from 'geist/font/sans';

Expand All @@ -23,16 +25,20 @@ export const metadata: Metadata = constructMetadata();
*/
export default function RootLayout({ children }: PropsWithChildren) {
return (
<html
lang="en"
suppressHydrationWarning
className={`${GeistSans.variable} ${GeistMono.variable}`}
<ClerkProvider
appearance={{ baseTheme: dark, variables: { colorPrimary: '#F9617B' } }}
>
<body>
<ThemeProvider attribute="class" disableTransitionOnChange>
{children}
</ThemeProvider>
</body>
</html>
<html
lang="en"
suppressHydrationWarning
className={`${GeistSans.variable} ${GeistMono.variable}`}
>
<body>
<ThemeProvider attribute="class" disableTransitionOnChange>
{children}
</ThemeProvider>
</body>
</html>
</ClerkProvider>
);
}
2 changes: 2 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const env = createEnv({
},
server: {
PORT: z.coerce.number().default(3000),
UPSTASH_REDIS_REST_URL: z.string().optional(),
UPSTASH_REDIS_REST_TOKEN: z.string().optional(),
},
client: {},
experimental__runtimeEnv: {
Expand Down
40 changes: 40 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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) => {
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));
}
});

export const config = {
matcher: ['/((?!.*\\..*|_next).*)', '/', '/blocked', '/(api|trpc)(.*)'],
};