Skip to content
Merged
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
67 changes: 34 additions & 33 deletions src/app/api/auth/liveblocks/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { auth, currentUser } from "@clerk/nextjs/server";
import { Liveblocks } from "@liveblocks/node";
import { ConvexHttpClient } from "convex/browser";
import { api } from "../../../../../convex/_generated/api";
// import { Liveblocks } from "@liveblocks/node";
// import { ConvexHttpClient } from "convex/browser";
// import { api } from "../../../../../convex/_generated/api";

const convex = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);
const liveblocks = new Liveblocks({
secret: process.env.LIVE_BLOCK_SECRET_API_KEY!,
});
// const convex = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);
// const liveblocks = new Liveblocks({
// secret: process.env.LIVE_BLOCK_SECRET_API_KEY!,
// });

export async function POST(req: Request) {
const { sessionClaims } = await auth();
Expand All @@ -16,30 +16,31 @@ export async function POST(req: Request) {
if (!user) return new Response("Unauthorized", { status: 401 });

const { room } = await req.json();
const document = await convex.query(api.document.get, {
id: room,
ignoreAuth: true,
});

if (!document) return new Response("Unauthorized", { status: 401 });

const isOwner = document.ownerId === user.id;
const isOrgMember = !!(
document.organizationId && document.organizationId === sessionClaims.org_id
);

if (!isOwner && !isOrgMember)
return new Response("Unauthorized", { status: 401 });

const session = liveblocks.prepareSession(user.id, {
userInfo: {
name:
user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous",
avatar: user.imageUrl,
},
});

session.allow(room, session.FULL_ACCESS);
const { body, status } = await session.authorize();
return new Response(body, { status });
// const document = await convex.query(api.document.get, {
// id: room,
// ignoreAuth: true,
// });

// if (!document) return new Response("Unauthorized", { status: 401 });

// const isOwner = document.ownerId === user.id;
// const isOrgMember = !!(
// document.organizationId && document.organizationId === sessionClaims.org_id
// );

// if (!isOwner && !isOrgMember)
// return new Response("Unauthorized", { status: 401 });

// const session = liveblocks.prepareSession(user.id, {
// userInfo: {
// name:
// user.fullName ?? user.primaryEmailAddress?.emailAddress ?? "Anonymous",
// avatar: user.imageUrl,
// },
// });

// session.allow(room, session.FULL_ACCESS);
// const { body, status } = await session.authorize();
// return new Response(body, { status });
return new Response(room, { status: 200 });
}
Loading