Function Signature
export const createDocument = async ({ userId , email } : CreateDocumentParams ) => Promise < Room | undefined >
Creates a new collaborative document room with the specified user as the owner. The document is initialized with write access for the creator and a default “Untitled” title.
Parameters
The unique identifier of the user creating the document. Must match the creator’s ID.
The email address of the user creating the document. Must match the authenticated user’s email.
Return Value
Returns the created room object with metadata and access configuration, or undefined if an error occurs. Unique room identifier (generated using nanoid)
Room metadata containing:
creatorId: User ID of the document creator
email: Creator’s email address
title: Document title (defaults to “Untitled”)
Access control mapping. Creator receives ['room:write'] permissions.
Usage Example
import { createDocument } from '@/lib/actions/room.actions' ;
const handleCreateDocument = async () => {
const newDocument = await createDocument ({
userId: 'user_2abc123' ,
email: '[email protected] '
});
if ( newDocument ) {
console . log ( 'Document created:' , newDocument . id );
}
};
Error Handling
The function handles several error cases:
Unauthorized Access : Throws an error if the authenticated user’s email doesn’t match the provided email parameter.
// Error: "Unauthorized to create document"
if ( ! currentEmail || currentEmail !== email ) {
throw new Error ( 'Unauthorized to create document' );
}
The function automatically revalidates the root path (/) after successful document creation to update the documents list.
Implementation Details
Room ID Generation : Uses nanoid() to generate unique room identifiers
Default Access : Creator receives write permissions (['room:write'])
Default Title : New documents are created with the title “Untitled”
Cache Revalidation : Automatically revalidates / path using Next.js revalidatePath
declare type CreateDocumentParams = {
userId : string ;
email : string ;
};
declare type RoomMetadata = {
creatorId : string ;
email : string ;
title : string ;
};
declare type RoomAccesses = Record < string , AccessType >;
declare type AccessType = [ "room:write" ] | [ "room:read" , "room:presence:write" ];
Source
Defined in lib/actions/room.actions.ts:20