CLERK Setup - pasalemaitri/CSYE7230-project GitHub Wiki
Money Mind AI Setup
Dhruv Patel edited this page on Apr 5, 2025 ยท 1 revision
๐ง Getting Started with Money Mind AI on Render with Clerk Authentication
This guide walks you through setting up Clerk authentication, webhook integration, and deploying Money Mind AI (built using Next.js and TypeScript) on Render.com.
๐ Step 1: Create a Clerk Account
- Visit https://clerk.com.
- Sign up or log in to your Clerk dashboard.
- Create a new application for Money Mind AI.
- Copy your Publishable Key and Secret Key from the app settings.
โ๏ธ Step 2: Integrate Clerk in Your Next.js App
Install Clerkโs Next.js package:
npm install @clerk/nextjs
Create a .env.local file in your project root and add the following:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=your_publishable_key
CLERK_SECRET_KEY=your_secret_key
Create a middleware file at src/middleware.ts:
import { authMiddleware } from "@clerk/nextjs";
export default authMiddleware();
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
๐ง Getting Started with Money Mind AI on Render with Clerk Authentication
This guide walks you through setting up Clerk authentication, webhook integration, and deploying Money Mind AI (built using Next.js and TypeScript) on Render.com.
๐ Step 3: Add Clerk Webhook Integration
Install required dependencies:
npm install svix @clerk/nextjs
Create the webhook handler route at src/app/api/webhooks/clerk/route.ts:
import { Webhook } from 'svix'
import { headers } from 'next/headers'
import { WebhookEvent } from '@clerk/nextjs/server'
export async function POST(req: Request) {
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET
if (!WEBHOOK_SECRET) {
throw new Error('Missing WEBHOOK_SECRET in environment')
}
const headerPayload = headers();
const svix_id = headerPayload.get("svix-id");
const svix_timestamp = headerPayload.get("svix-timestamp");
const svix_signature = headerPayload.get("svix-signature");
if (!svix_id || !svix_timestamp || !svix_signature) {
return new Response('Missing SVIX headers', { status: 400 });
}
const body = JSON.stringify(await req.json());
const wh = new Webhook(WEBHOOK_SECRET);
let event: WebhookEvent;
try {
event = wh.verify(body, {
"svix-id": svix_id,
"svix-timestamp": svix_timestamp,
"svix-signature": svix_signature,
}) as WebhookEvent;
} catch (error) {
console.error('Webhook verification failed:', error);
return new Response('Invalid signature', { status: 400 });
}
if (event.type === 'user.created') {
console.log('๐ค New user registered:', event.data);
// Add custom logic here
}
return new Response('', { status: 200 });
}
๐ Step 4: Register Webhook on Clerk
-
In the Clerk dashboard, go to the Webhooks tab.
-
Click "Add Endpoint".
-
Enter the following URL:
https://your-moneymind-ai.onrender.com/api/webhooks/clerk
-
Choose relevant events (e.g., user.created).
-
Save the endpoint and copy the WEBHOOK_SECRET.
๐ Step 5: Deploy to Render.com
-
Go to Render.com and create a new Web Service.
-
Link your GitHub repository that contains the Money Mind AI code.
-
Configure the following build settings:
Build Command:
npm run build
Start Command:
npm start
- Add the following environment variables:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=your_publishable_key
CLERK_SECRET_KEY=your_secret_key
WEBHOOK_SECRET=your_webhook_secret
- Deploy and monitor your build logs for successful deployment.
โ Step 6: Test Your Webhook
-
In the Clerk Dashboard, go to your Webhook endpoint.
-
Use "Send Example Event" to trigger a test.
-
Open your Render logs and confirm the event was successfully received and processed.
๐ก Youโre all set!
Clerk is now fully integrated with Money Mind AI, and youโre ready to build secure, smart finance features!
--