By default, Mirin will store and load your end-user documents on our servers throughout a session.

If you prefer to handle this yourself, you can easily do so by specifying a custom endpoint in your embed configuration.

Set up

First, login to your account and specify a custom POST endpoint in your Embed’s settings.

Then create the endpoint on your server:

// server.tsx
app.post('/mirin-session', (req) => {
  // The payload object will contain the action that
  // the client is trying to perform (ie: storing a document, loading a document etc)
  const payload = req.json('payload');

  switch(payload.type) {
    ...
  }
});

Authentication

When we load the <Embed /> component on your frontend, recall that it will need to perform an authentication on your server to generate a valid token:

// frontend/editor.tsx
<Embed 
  authenticate={async () => {
    await fetch('/authenticate-embed', {
      method: "POST"
    })
  }}
/>

Then on your server, you will typically call Mirin’s client.authenticate:

// backend/server.ts
import { Client } from '@mirinhq/admin';

const client = new Client({
  apiKey: '...'
});

app.post('/authenticate-embed', () => {
  return client.authorize();
})

The client.authorize() method accepts a context argument where you can pass additional information that you may need to throughout a session. This context argument will be passed whenever your endpoint is called.

For example:

app.post('/authenticate-embed', () => {
  const context = {
    documentId: '...' // get the document id from your database
  }

  return client.authorize(context);
})

Loading a Document

When Mirin tries to load a document to display to the user on the editor, the GetDocument payload will be sent your endpoint:

app.post('/mirin-session', (req) => {
  // ...

  switch(payload.type) {
    case 'GetDocument': {
      // Context information from client.authorize(context)
      const context = payload.context;

      const { documentId } = context;

      const document = {} // fetch the document's metadata and content from your server/database

      return document;
    }
  }
});

Storing a Document

When Mirin tries to store a document that the user is editing, the UpdateDocumentContent payload will be sent your endpoint:

app.post('/mirin-session', (req) => {
  // ...

  switch(payload.type) {
    case 'UpdateDocumentContent': {
      const { context, content } = payload;

      const { documentId } = context;

      // Update the document's content on your server/database

      return true;
    }
  }
});