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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/server-side-sub-org-lookup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@monad-inc/embed': patch
---

docs: look up the sub-org on the server in the session example, instead of trusting a sub-org id sent by the browser, and note session expiry
30 changes: 26 additions & 4 deletions packages/embed/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,30 @@ iframe using that token. Both shown below.
Your backend exchanges its long-lived Monad API key for a short-lived,
sub-org-scoped session token, then hands that token to your frontend.

Your backend decides which sub-org the session is for. Look it up from
the signed-in user's own session, never from anything the browser sends:
a sub-org id in the request body could be changed to another
customer's.

Example (any HTTP framework — shown here as a Next.js Route Handler):

```ts
// app/api/monad/session/route.ts
export async function POST(req: Request) {
const { subOrgId } = await req.json(); // your tenant → Monad sub-org mapping
// Your own auth: who is signed in, and which of your tenants they belong to.
const user = await getSignedInUser(req);
if (!user) return new Response(null, { status: 401 });

// Your server-side tenant → Monad sub-org mapping, stored when you
// created the customer's sub-org.
const subOrgId = await lookUpMonadSubOrg(user.tenantId);
if (!subOrgId) return new Response(null, { status: 404 });

const r = await fetch('https://app.monad.com/api/v3/sessions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'your-product/1.0',
Authorization: `ApiKey ${process.env.MONAD_API_KEY}`
},
body: JSON.stringify({
Expand All @@ -69,17 +82,26 @@ Returns:
The `session_token` is what your frontend hands to `createConnectorFrame`.
**Never put your long-lived `MONAD_API_KEY` in browser code.**

- **Always send `organization_id`.** A session created without it gets
the full access of the API key that created it.
- **If one user can work in several of your tenants**, the browser may
say which tenant it wants, but check on your server that the
signed-in user belongs to it before minting, and return 403 if not.
- **Sessions expire** (`ttl_seconds`, default 1800). The form takes the
token once, at mount, so keep `expires_at` and mint a new session and
remount the form before it expires.

---

## Step 2 — Mount the iframe on your frontend

```ts
import { createConnectorFrame } from '@monad-inc/embed/connect';

// Fetch a fresh session from your own backend.
// Fetch a fresh session from your own backend. Send no sub-org id:
// your backend looks it up from the signed-in user.
const { session_token, organizationId } = await fetch('/api/monad/session', {
method: 'POST',
body: JSON.stringify({ subOrgId: currentUser.subOrgId })
method: 'POST'
}).then((r) => r.json());

const frame = createConnectorFrame({
Expand Down
Loading