Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
33 changes: 33 additions & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Every key here is optional: with none set the app runs — Clerk as a sample
# user, BaseHub as sample content, the rest switched off — and says what to
# connect. For `next dev`, copy this to .env.local. Under `wrangler dev`
# (`pnpm preview`) the server-only keys go in .dev.vars instead. Deployed,
# each is an Orun secret of the same name (component.yaml).
# Server
CLERK_SECRET_KEY=""
CLERK_WEBHOOK_SECRET=""
RESEND_FROM=""
DATABASE_URL="postgres://postgres:postgres@localhost:54322/postgres"
RESEND_TOKEN=""
STRIPE_SECRET_KEY=""
STRIPE_WEBHOOK_SECRET=""
BETTERSTACK_API_KEY=""
BETTERSTACK_URL=""
FLAGS_SECRET=""
SVIX_TOKEN=""
LIVEBLOCKS_SECRET=""
BASEHUB_TOKEN=""
KNOCK_API_KEY=""
KNOCK_FEED_CHANNEL_ID=""

# Client
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=""
NEXT_PUBLIC_CLERK_SIGN_IN_URL="/sign-in"
NEXT_PUBLIC_CLERK_SIGN_UP_URL="/sign-up"
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL="/"
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL="/"
NEXT_PUBLIC_GA_MEASUREMENT_ID=""
NEXT_PUBLIC_POSTHOG_KEY=""
NEXT_PUBLIC_POSTHOG_HOST=""
NEXT_PUBLIC_APP_URL="http://localhost:3000"
NEXT_PUBLIC_WEB_URL="http://localhost:3001"
7 changes: 7 additions & 0 deletions apps/api/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The origins the three Workers link to each other by, inlined at build time
# (NEXT_PUBLIC_). These are stage's workers.dev URLs, for a local
# `pnpm build`; every Orun lane builds with its own environment's
# (component.yaml's buildCommand), which wins over this file.
NEXT_PUBLIC_APP_URL=https://next-forge-app-stage.your-workers-subdomain.workers.dev
NEXT_PUBLIC_WEB_URL=https://next-forge-web-stage.your-workers-subdomain.workers.dev
NEXT_PUBLIC_API_URL=https://next-forge-api-stage.your-workers-subdomain.workers.dev
4 changes: 4 additions & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# local state wrangler and the Cloudflare adapter write
.wrangler/
.open-next/
.dev.vars
Binary file added apps/api/app/apple-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions apps/api/app/cron/keep-alive/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { database } from "@repo/database";

export const GET = async () => {
const newPage = await database.page.create({
data: {
name: "cron-temp",
},
});

await database.page.delete({
where: {
id: newPage.id,
},
});

return new Response("OK", { status: 200 });
};
29 changes: 29 additions & 0 deletions apps/api/app/global-error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use client";

import { Button } from "@repo/design-system/components/ui/button";
import { fonts } from "@repo/design-system/lib/fonts";
import { captureException } from "@repo/observability/sentry";
import type NextError from "next/error";
import { useEffect } from "react";

interface GlobalErrorProperties {
readonly error: NextError & { digest?: string };
readonly reset: () => void;
}

const GlobalError = ({ error, reset }: GlobalErrorProperties) => {
useEffect(() => {
captureException(error);
}, [error]);

return (
<html className={fonts} lang="en">
<body>
<h1>Oops, something went wrong</h1>
<Button onClick={() => reset()}>Try again</Button>
</body>
</html>
);
};

export default GlobalError;
1 change: 1 addition & 0 deletions apps/api/app/health/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const GET = (): Response => new Response("OK", { status: 200 });
Binary file added apps/api/app/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions apps/api/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AnalyticsProvider } from "@repo/analytics/provider";
import type { ReactNode } from "react";

interface RootLayoutProperties {
readonly children: ReactNode;
}

const RootLayout = ({ children }: RootLayoutProperties) => (
<html lang="en">
<body>
<AnalyticsProvider>{children}</AnalyticsProvider>
</body>
</html>
);

export default RootLayout;
Binary file added apps/api/app/opengraph-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
233 changes: 233 additions & 0 deletions apps/api/app/webhooks/auth/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { analytics } from "@repo/analytics/server";
import type {
DeletedObjectJSON,
OrganizationJSON,
OrganizationMembershipJSON,
UserJSON,
WebhookEvent,
} from "@repo/auth/server";
import { log } from "@repo/observability/log";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { Webhook } from "svix";
import { env } from "@/env";

const handleUserCreated = (data: UserJSON) => {
analytics?.identify({
distinctId: data.id,
properties: {
email: data.email_addresses.at(0)?.email_address,
firstName: data.first_name,
lastName: data.last_name,
createdAt: new Date(data.created_at),
avatar: data.image_url,
phoneNumber: data.phone_numbers.at(0)?.phone_number,
},
});

analytics?.capture({
event: "User Created",
distinctId: data.id,
});

return new Response("User created", { status: 201 });
};

const handleUserUpdated = (data: UserJSON) => {
analytics?.identify({
distinctId: data.id,
properties: {
email: data.email_addresses.at(0)?.email_address,
firstName: data.first_name,
lastName: data.last_name,
createdAt: new Date(data.created_at),
avatar: data.image_url,
phoneNumber: data.phone_numbers.at(0)?.phone_number,
},
});

analytics?.capture({
event: "User Updated",
distinctId: data.id,
});

return new Response("User updated", { status: 201 });
};

const handleUserDeleted = (data: DeletedObjectJSON) => {
if (data.id) {
analytics?.identify({
distinctId: data.id,
properties: {
deleted: new Date(),
},
});

analytics?.capture({
event: "User Deleted",
distinctId: data.id,
});
}

return new Response("User deleted", { status: 201 });
};

const handleOrganizationCreated = (data: OrganizationJSON) => {
analytics?.groupIdentify({
groupKey: data.id,
groupType: "company",
distinctId: data.created_by,
properties: {
name: data.name,
avatar: data.image_url,
},
});

if (data.created_by) {
analytics?.capture({
event: "Organization Created",
distinctId: data.created_by,
});
}

return new Response("Organization created", { status: 201 });
};

const handleOrganizationUpdated = (data: OrganizationJSON) => {
analytics?.groupIdentify({
groupKey: data.id,
groupType: "company",
distinctId: data.created_by,
properties: {
name: data.name,
avatar: data.image_url,
},
});

if (data.created_by) {
analytics?.capture({
event: "Organization Updated",
distinctId: data.created_by,
});
}

return new Response("Organization updated", { status: 201 });
};

const handleOrganizationMembershipCreated = (
data: OrganizationMembershipJSON
) => {
analytics?.groupIdentify({
groupKey: data.organization.id,
groupType: "company",
distinctId: data.public_user_data.user_id,
});

analytics?.capture({
event: "Organization Member Created",
distinctId: data.public_user_data.user_id,
});

return new Response("Organization membership created", { status: 201 });
};

const handleOrganizationMembershipDeleted = (
data: OrganizationMembershipJSON
) => {
// Need to unlink the user from the group

analytics?.capture({
event: "Organization Member Deleted",
distinctId: data.public_user_data.user_id,
});

return new Response("Organization membership deleted", { status: 201 });
};

export const POST = async (request: Request): Promise<Response> => {
if (!env.CLERK_WEBHOOK_SECRET) {
return NextResponse.json({ message: "Not configured", ok: false });
}

// Get the headers
const headerPayload = await headers();
const svixId = headerPayload.get("svix-id");
const svixTimestamp = headerPayload.get("svix-timestamp");
const svixSignature = headerPayload.get("svix-signature");

// If there are no headers, error out
if (!(svixId && svixTimestamp && svixSignature)) {
return new Response("Error occured -- no svix headers", {
status: 400,
});
}

// Get the body
const payload = (await request.json()) as object;
const body = JSON.stringify(payload);

// Create a new SVIX instance with your secret.
const webhook = new Webhook(env.CLERK_WEBHOOK_SECRET);

let event: WebhookEvent | undefined;

// Verify the payload with the headers
try {
event = webhook.verify(body, {
"svix-id": svixId,
"svix-timestamp": svixTimestamp,
"svix-signature": svixSignature,
}) as WebhookEvent;
} catch (error) {
log.error("Error verifying webhook:", { error });
return new Response("Error occured", {
status: 400,
});
}

// Get the ID and type
const { id } = event.data;
const eventType = event.type;

log.info("Webhook", { id, eventType, body });

let response: Response = new Response("", { status: 201 });

switch (eventType) {
case "user.created": {
response = handleUserCreated(event.data);
break;
}
case "user.updated": {
response = handleUserUpdated(event.data);
break;
}
case "user.deleted": {
response = handleUserDeleted(event.data);
break;
}
case "organization.created": {
response = handleOrganizationCreated(event.data);
break;
}
case "organization.updated": {
response = handleOrganizationUpdated(event.data);
break;
}
case "organizationMembership.created": {
response = handleOrganizationMembershipCreated(event.data);
break;
}
case "organizationMembership.deleted": {
response = handleOrganizationMembershipDeleted(event.data);
break;
}
default: {
break;
}
}

await analytics?.shutdown();

return response;
};
Loading
Loading