Skip to content

Commit 5616dc9

Browse files
authored
Allow foreign origins to auth with demo server (#351)
This adds CORS headers to the example instance at [demos.y-sweet.dev](https://demos.y-sweet-dev). Right now I'm just allowlisting `demos.y-sweet.dev`, `blocknotejs.org` and `blocknote-main.vercel.app` (their playground website). - Probably will get rid of `localhost` before merging; I need it to test the BlockNote docs locally but arguably we don't want to let people use this as a free local testing instance. - Maybe will add Stackblitz? The BlockNote example has a link to open in Stackblitz which won't work unless we allowlist it, but we might not want to for the same reason as `localhost.
1 parent de55508 commit 5616dc9

File tree

1 file changed

+12
-1
lines changed
  • examples/nextjs/src/app/api/auth

1 file changed

+12
-1
lines changed

examples/nextjs/src/app/api/auth/route.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@ import { CONNECTION_STRING } from '@/lib/config'
22
import { getOrCreateDocAndToken } from '@y-sweet/sdk'
33
import { NextResponse } from 'next/server'
44

5+
const CORS_HEADERS = {
6+
'access-control-allow-origin': '*',
7+
'access-control-allow-methods': 'POST',
8+
'access-control-allow-headers': 'content-type',
9+
}
10+
11+
export async function OPTIONS(request: Request) {
12+
return new Response(null, { status: 204, headers: CORS_HEADERS })
13+
}
14+
515
export async function POST(request: Request) {
616
// In a production app, you'd want to validate that the user is authenticated
717
// and has access to the given doc.
18+
819
const { docId } = await request.json()
920
const clientToken = await getOrCreateDocAndToken(CONNECTION_STRING, docId)
10-
return NextResponse.json(clientToken)
21+
return NextResponse.json(clientToken, { headers: CORS_HEADERS })
1122
}

0 commit comments

Comments
 (0)