Skip to main content

Function Signature

export const getDocument = async ({ roomId, userId }: { roomId: string; userId: string }) => Promise<Room | undefined>
Retrieves a collaborative document room by its ID, with access control verification to ensure the user has permission to view the document.

Parameters

roomId
string
required
The unique identifier of the document room to retrieve.
userId
string
required
The user ID (email) of the user requesting access to the document. Must match the authenticated user’s email.

Return Value

room
Room | undefined
Returns the room object with metadata and access configuration, or undefined if an error occurs.

Usage Example

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

const loadDocument = async (roomId: string) => {
  const document = await getDocument({
    roomId: roomId,
    userId: '[email protected]'
  });
  
  if (document) {
    console.log('Document title:', document.metadata.title);
    console.log('Has access:', Object.keys(document.usersAccesses));
  }
};

Error Handling

The function performs two levels of authorization:
Authentication Check: Verifies the authenticated user’s email matches the provided userId.
// Error: "Unauthorized to access this document"
if (!currentEmail || currentEmail !== userId) {
  throw new Error('Unauthorized to access this document');
}
Access Control Check: Verifies the user has explicit access to the room.
// Error: "You do not have access to this document"
const hasAccess = Object.keys(room.usersAccesses).includes(userId);
if (!hasAccess) {
  throw new Error('You do not have access to this document');
}

Security

This function implements dual-layer security:
  1. Validates the authenticated user matches the requesting user
  2. Checks the user exists in the room’s access control list

Implementation Details

  • Access Verification: Checks if userId exists in room.usersAccesses keys
  • Email Validation: Compares authenticated email with userId parameter
  • No Side Effects: This is a read-only operation with no cache revalidation

Source

Defined in lib/actions/room.actions.ts:54

Build docs developers (and LLMs) love