Anonymous users
Let people write before they sign up, then carry their documents, credit, keys and agents over to their account when they do.
Many apps let people start writing before they make an account, or let a visitor who opened a shared link comment without signing up. Undertext doesn't mind who your users are, only that you give each one an id of yours, so an anonymous visitor is just another user to Undertext. By the end of this guide, visitors can write straight away, and everything they did moves to their account when they sign up.
Give visitors a user
-
Make an id when a visitor first needs one, such as when they start a document or open a shared link, and keep it in your own session. It becomes the user's
external_idat Undertext. Any string up to 128 characters works; a prefix makes these easy to spot.session.ts async function currentUser(req) { if (req.user) return req.user; if (!req.session.anonymousId) { req.session.anonymousId = `anon_${crypto.randomUUID()}`; req.session.anonymousName = randomName(); // "Brisk Heron" } return { id: req.session.anonymousId, name: req.session.anonymousName, anonymous: true }; } -
Create the user in Undertext with a name that reads well in the margin and in comments, and keep the Undertext id in the session beside your own. A made-up name is friendlier than "Anonymous", and lets people tell two visitors apart.
const visitor = await currentUser(req); if (!req.session.undertextUserId) { const undertextUser = await undertext.users.upsert(visitor.id, { name: visitor.name }); req.session.undertextUserId = undertextUser.id; } -
Use them like any other user. Create documents they own, start editor sessions, and start link sessions for shared documents. Everything they write is credited to their made-up name.
Anonymous users can do everything signed-in users can in Undertext: write, comment, add security keys, connect agents and seal. Which of those your app allows before sign-up is up to you.
Merge when they sign up
When a visitor signs up or signs in, merge their anonymous user into their account:
app.post("/sign-in/complete", async (req, res) => {
const account = await finishSignIn(req); // your own sign-in
if (req.session.undertextUserId) {
// undertextUserId: the helper from Embed the editor, which creates the account's Undertext user if needed.
await undertext.users.merge(req.session.undertextUserId, { into: await undertextUserId(account) });
delete req.session.anonymousId;
delete req.session.undertextUserId;
}
res.redirect("/notes");
});Merging moves, from the anonymous user to the account:
- Documents and memberships. Where both were members of the same document, the account keeps the higher role.
- Credit. Everything the visitor wrote, commented and suggested now shows under the account's name, in open editors too.
- Security keys and their check-ins, and the documents the visitor turned Verified Presence on in.
- Agents, still reaching the same documents. One for a client the account already connected joins that agent.
The anonymous user is then gone: its Undertext id answers 404, and its external_id is free. Existing seals keep the name they were sealed with, as seals always do.
Merge before you start the next editor session. An editor still open as the anonymous user loses its session when the merge completes and asks your page for a new token; once your session route starts sessions as the account, it picks up where it left off.
Clean up visitors who never come back
Anonymous users stay until you delete them, by the Undertext id you kept for each:
await undertext.users.delete("usr_2Hd7Np4Q");Deleting a user deletes documents nobody else is a member of or wrote in, with their seals. In documents shared with others, what the visitor wrote keeps the name they last had, without a picture.
Next
Send notifications
Receive Undertext's webhooks, check their signatures, and turn comments, mentions and seals into your own notifications and emails.
Add your own agent tools
Give agents your app's own features, such as folders and pins, as tools Undertext's MCP server lists beside its own and forwards to your server.