11import { auth , currentUser } from "@clerk/nextjs/server" ;
2- // import { Liveblocks } from "@liveblocks/node";
3- // import { ConvexHttpClient } from "convex/browser";
4- // import { api } from "../../../../../convex/_generated/api";
5-
6- // const convex = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);
7- // const liveblocks = new Liveblocks({
8- // secret: process.env.LIVE_BLOCK_SECRET_API_KEY!,
9- // });
2+ import { Liveblocks } from "@liveblocks/node" ;
3+ import { ConvexHttpClient } from "convex/browser" ;
4+ import { api } from "../../../../../convex/_generated/api" ;
5+
6+ // Ensure these environment variables are defined in your GitHub Actions workflow
7+ // Add error handling for missing environment variables
8+ const convex = new ConvexHttpClient (
9+ process . env . NEXT_PUBLIC_CONVEX_URL ??
10+ ( ( ) => { throw new Error ( "NEXT_PUBLIC_CONVEX_URL environment variable is not defined" ) } ) ( )
11+ ) ;
12+
13+ const liveblocks = new Liveblocks ( {
14+ secret : process . env . LIVE_BLOCK_SECRET_API_KEY ??
15+ ( ( ) => { throw new Error ( "LIVE_BLOCK_SECRET_API_KEY environment variable is not defined" ) } ) ( )
16+ } ) ;
1017
1118export async function POST ( req : Request ) {
1219 const { sessionClaims } = await auth ( ) ;
@@ -16,31 +23,30 @@ export async function POST(req: Request) {
1623 if ( ! user ) return new Response ( "Unauthorized" , { status : 401 } ) ;
1724
1825 const { room } = await req . json ( ) ;
19- // const document = await convex.query(api.document.get, {
20- // id: room,
21- // ignoreAuth: true,
22- // });
23-
24- // if (!document) return new Response("Unauthorized", { status: 401 });
25-
26- // const isOwner = document.ownerId === user.id;
27- // const isOrgMember = !!(
28- // document.organizationId && document.organizationId === sessionClaims.org_id
29- // );
30-
31- // if (!isOwner && !isOrgMember)
32- // return new Response("Unauthorized", { status: 401 });
33-
34- // const session = liveblocks.prepareSession(user.id, {
35- // userInfo: {
36- // name:
37- // user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous",
38- // avatar: user.imageUrl,
39- // },
40- // });
41-
42- // session.allow(room, session.FULL_ACCESS);
43- // const { body, status } = await session.authorize();
44- // return new Response(body, { status });
45- return new Response ( room , { status : 200 } ) ;
26+ const document = await convex . query ( api . document . get , {
27+ id : room ,
28+ ignoreAuth : true ,
29+ } ) ;
30+
31+ if ( ! document ) return new Response ( "Unauthorized" , { status : 401 } ) ;
32+
33+ const isOwner = document . ownerId === user . id ;
34+ const isOrgMember = ! ! (
35+ document . organizationId && document . organizationId === sessionClaims . org_id
36+ ) ;
37+
38+ if ( ! isOwner && ! isOrgMember )
39+ return new Response ( "Unauthorized" , { status : 401 } ) ;
40+
41+ const session = liveblocks . prepareSession ( user . id , {
42+ userInfo : {
43+ name :
44+ user . fullName ?? user . primaryEmailAddress ?. emailAddress ?? "Anonymous" ,
45+ avatar : user . imageUrl ,
46+ } ,
47+ } ) ;
48+
49+ session . allow ( room , session . FULL_ACCESS ) ;
50+ const { body, status } = await session . authorize ( ) ;
51+ return new Response ( body , { status } ) ;
4652}
0 commit comments