Skip to main content

Function Signature

export const getDocuments = async (email: string) => Promise<{ data: Room[] } | undefined>
Retrieves all Liveblocks rooms (documents) that the specified user has access to. Returns both documents owned by the user and documents shared with them.

Parameters

email
string
required
The email address of the user whose documents to retrieve. Must match the authenticated user’s email for authorization.

Return Value

data
Room[]
Array of room objects representing documents accessible to the user.

Usage Example

import { getDocuments } from '@/lib/actions/room.actions';

// Get all documents for a user
const userEmail = '[email protected]';
const result = await getDocuments(userEmail);

if (result) {
  const documents = result.data;
  
  documents.forEach(doc => {
    console.log(`Document: ${doc.metadata.title}`);
    console.log(`Created: ${doc.createdAt}`);
    console.log(`Room ID: ${doc.id}`);
  });
}

Display Documents in UI

From app/(root)/page.tsx:
const Home = async () => {
  const clerkUser = await currentUser();
  const email = clerkUser.emailAddresses[0]?.emailAddress;
  
  const roomDocuments = await getDocuments(email);

  return (
    <ul className="document-ul">
      {roomDocuments.data.map(({ id, metadata, createdAt }) => (
        <li key={id}>
          <Link href={`/documents/${id}`}>
            <p>{metadata.title}</p>
            <p>Created about {dateConverter(createdAt)}</p>
          </Link>
          <DeleteModal roomId={id} />
        </li>
      ))}
    </ul>
  );
}

Authorization

The function validates that the requesting user’s email matches the provided email parameter. This prevents users from accessing other users’ document lists.
Authentication check (line 107-111):
const currentEmail = await getCurrentUserEmail();

if (!currentEmail || currentEmail !== email) {
  throw new Error('Unauthorized to list documents');
}

Implementation Details

  • Uses Liveblocks getRooms() API with userId parameter set to the user’s email
  • Returns documents where the user is either the creator or a collaborator
  • Results are serialized with parseStringify() for Next.js compatibility
  • Errors are logged but not re-thrown (returns undefined on error)

Source Reference

lib/actions/room.actions.ts:105-119

Build docs developers (and LLMs) love