Skip to content

Commit

Permalink
feat: dashboard page initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
ixahmedxi committed May 25, 2024
1 parent 7c53430 commit 2a5ea73
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 39 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"@radix-ui/react-navigation-menu": "^1.1.4",
"@radix-ui/react-slot": "^1.0.2",
"@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",
Expand Down
15 changes: 15 additions & 0 deletions src/app/(dashboard)/app/page.tsx
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>
);
}
8 changes: 7 additions & 1 deletion src/app/(marketing)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Link from 'next/link';
import { redirect } from 'next/navigation';

import { currentUser } from '@clerk/nextjs/server';
import { ChevronRightIcon, StarIcon } from 'lucide-react';

import { constants } from '@/constants';
Expand All @@ -12,7 +14,11 @@ import { HomePreview } from './_components/home-preview';
*
* @returns A react component representing the marketing home page.
*/
export default function Home() {
export default async function Home() {
const session = await currentUser();

if (session) redirect('/app');

return (
<main className="flex flex-col items-center justify-center gap-6 pt-24">
<Button variant="outline" asChild className="rounded-full font-normal">
Expand Down
8 changes: 4 additions & 4 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const env = createEnv({
PORT: z.coerce.number().default(3000),

// upstash
UPSTASH_REDIS_REST_URL: z.string().optional(),
UPSTASH_REDIS_REST_TOKEN: z.string().optional(),
UPSTASH_REDIS_REST_URL: z.string(),
UPSTASH_REDIS_REST_TOKEN: z.string(),

// Clerk
CLERK_SECRET_KEY: z.string().min(1),
CLERK_SECRET_KEY: z.string(),
},
client: {
// Clerk
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string().min(1),
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string(),
},

experimental__runtimeEnv: {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/redis.ts
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,
});
35 changes: 2 additions & 33 deletions src/middleware.ts
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)(.*)'],
};

0 comments on commit 2a5ea73

Please sign in to comment.