Files
puter/doc/embedding-app-browser.md
T
Nariman Jelveh 2f8f81e621 Bind the app browser embed to the host's user and add <puter-user-menu> (#4528)
* Show the app browser embed's session only to the host's signed-in user

/embed/apps now reads the host page's signed-in user from its fragment
(#user=<uuid>) and asks /whoami which user the GUI session belongs to.
It shows the user's apps only when the two match. A host that names no
user, a malformed one, or another user, and a session the API refuses,
get the signed-out view of recommended apps with no SDK and no app
tokens. A failed /whoami check shows the unavailable state. Changing the
fragment reloads the embed.

Hosts pass the uuid from puter.auth.getUser() and remove the iframe when
their own sign-in changes; the doc's example does both.

* Add <puter-user-menu> for first-party apps on Puter subdomains

puter.js registers a <puter-user-menu> element that follows the page's
own puter.js sign-in. Signed out it shows Sign in; signed in it shows the
user's profile picture, or their initial when there is none, with a menu
holding the username and Sign out. On a page one label under the Puter
origin, an Apps button opens /embed/apps in a modal with the signed-in
user named in the fragment, so the app browser shows the Puter session's
apps only to that user. Any change to the page's sign-in closes the app
browser. The element renders nothing outside a top-level page.

Its labels can be set with apps-label, sign-in-label, sign-out-label,
close-label and account-label, and it fires signin and signout events.
It is documented internally in components.md and the embed doc, and is
left out of the public docs because the app browser only works on Puter
subdomains.

* Show <puter-user-menu>'s app browser in a panel under its button

The Apps view and the account menu are now panels that hang under their
button with a caret, closing on Escape, an outside press or the button.
They use the top layer where the browser has one, so a host's transform,
overflow or z-index can't clip or cover them. On screens up to 600px wide
the Apps panel spans the width below the button.

Closing the Apps panel keeps the app browser loaded; a change to the
page's sign-in still discards it. A spinner shows until it loads, and its
colour scheme follows the component's theme, which follows the OS unless
the theme attribute is set.

A temporary account counts as signed out: it gets Sign in, no profile
picture request, and an app browser with no user named. The signin event
carries the signed-in user, and the component exposes the parts
apps-button, avatar, sign-in, menu and apps-panel for host styling.

* Read the user <puter-user-menu> signs in as after the SDK starts its whoami

setAuthToken notifies auth listeners before it starts the new token's
/whoami, so a sign-in on a loaded page showed '?' and opened the app
browser without naming the user.

* Close <puter-user-menu>'s account menu when Tab leaves it

Focus moved on to the page with the menu still open, where Escape no
longer reached it. Tab now closes the menu and moves on from its button.

* Move focus to Sign in after signing out from <puter-user-menu>

The focused menu and avatar disappear on sign-out, which dropped focus
to the page body.

* Show the app browser in the theme <puter-user-menu> forces

Chromium doesn't pass an iframe's color-scheme on to the page inside it,
so a forced theme that differed from the OS showed the app browser in
the OS's theme inside a panel in the forced one. The component now
passes a forced theme as theme= in the fragment, and the embed applies
it to its color-scheme rules.

* Show <puter-user-menu>'s account before its profile picture loads

The avatar and Apps button waited for the /profile request as well as
the user, so a slow profile kept them hidden. The picture is now added
when it arrives.

* Open one sign-in popup at a time from <puter-user-menu>

Clicking Sign in again while its popup is still open opened another popup each time. Further clicks are ignored until the sign-in finishes or its popup is closed. A cross-origin isolated page can't see its popup close, so it keeps the old behavior rather than locking the button.

* Keep <puter-user-menu>'s Apps button unshaded after a tap closes it

Touch screens keep :hover on the last tapped element, so tapping the Apps button to close the panel left it shaded as if the panel were still open. Hover shading now applies only where the device can hover; the open state still shades the button everywhere.

* Show <puter-user-menu>'s Sign in pressed and its account hovered

Sign in had no pressed state and the account button no hover state, so neither responded to the pointer. Sign in now takes the pressed look of Puter's other primary buttons, and the account button shows its open-menu ring on hover where the device can hover.

* Show a spinner in the app browser embed until its apps arrive

The embed stayed blank while it checked the session with /whoami and loaded the SDK. <puter-user-menu> hides its own spinner once the embed's page loads, so its panel went blank for that whole time. The empty embed now shows the same spinner in the same place until it renders its apps or its unavailable message.
2026-11-06 22:18:38 -07:00

8.1 KiB

Embedded app browser

GET /embed/apps on the deployment's main GUI origin renders the same app browser as the dashboard's Apps tab, without the desktop or dashboard navigation.

It supports search, paging, saved folders, and saved ordering. It uses the same installed-app and recommendation data as the dashboard, including the user's removed recommendations. Editing, adding, reordering, renaming, and uninstalling are disabled. Selecting an app opens it in a new browser tab at its own URL, the same way the dashboard does, and the host page stays open. The app's token isn't put in the URL: the app's SDK asks the embed for it once the tab loads, so the embed must still be open then. If the iframe is removed first, the app falls back to its normal sign-in. Apps that need the desktop (built-in and godmode apps, private apps the user can't open, or a failed token request) open /app/<name> on the GUI origin, cut off from the embed. Website shortcuts open their external destination with no opener. The embed follows the OS's light or dark setting. A host with a fixed theme adds theme=dark or theme=light to the fragment (#user=<uuid>&theme=dark), since Chromium doesn't pass an iframe's color-scheme on to the page inside it. Apps opened from the embed carry utm_source=puter.com&utm_medium=referral&utm_campaign=app_browser_embed; the desktop and dashboard send utm_campaign=desktop and utm_campaign=dashboard.

Session and framing

The embed shows the GUI session only to the user the host page is signed in as. The host names that user's uuid (from puter.auth.getUser()) in the iframe URL's fragment, as /embed/apps#user=<uuid>. The embed reads the session, asks /whoami which user it belongs to, and shows the signed-out view unless the two match. So a host that names no user, a malformed one, or a user other than the session's, gets the signed-out view, and so does a session the API refuses. A failed /whoami check shows the error state. The fragment never reaches the server and can only narrow what the embed shows: naming the session's own user gives the same view as a direct visit to the GUI. Changing the fragment of a loaded embed reloads it.

The embed reads the existing GUI session from local storage on its own origin. If that storage holds no session, the embed gets the session from the GUI origin's session cookie through /get-gui-token. This matters in Safari, which gives a framed origin separate local storage under each top-level origin, so even a sibling subdomain's iframe can't see the GUI's stored token. The session cookie is still sent, because the host is on the same site. The embed doesn't write that token back to storage. It does not create accounts, show a login dialog, or accept a token from its parent. Being signed in on a subdomain alone does not create a session on the main GUI origin.

Without a session it can show, the embed shows the recommended apps (RecommendedAppsService) that the server put in the page, and nothing else: no installed apps, folders, or saved order, and no puter.js. Selecting one opens it at the same URL a signed-in launch uses, with no opener and no token on offer, so the app signs in on its own. Apps that need the desktop open /app/<name>. If the page carries no recommended apps, or a load fails, the embed displays an unavailable/error state.

The route's Content-Security-Policy: frame-ancestors allows the GUI origin and the embedding page's origin when it is exactly one label under config.domain with the scheme and port from config.origin. Subdomains the server answers itself (api, dav, js, www, the native-app hosts, and the host of api_base_url) and the user-content hosting domains (static_hosting_domain, private_app_hosting_domain, and their _alt variants) are refused. For example, https://puter.com permits https://spreadsheet.puter.com but not https://api.puter.com, and http://puter.localhost:4100 permits http://spreadsheet.puter.localhost:4100 but not http://someone.site.puter.localhost:4100. The embedding page is read from the iframe request's referrer, so the host must not suppress it (the browser default strict-origin-when-cross-origin is enough). All ancestors in a nested iframe chain must be allowed. Other GUI routes retain X-Frame-Options: SAMEORIGIN.

The embed accepts no query parameters. Requests with a query string redirect to the same embed path so SDK bootstrap parameters cannot override the session or API origin. The host needs to allow the GUI origin in its own frame-src CSP if it sets one. If sandboxing the iframe, allow scripts, same-origin storage, popups, and popups to escape the sandbox.

Host integration

A first-party app that signs in with puter.js can place <puter-user-menu> from the SDK in its header instead (see src/puter-js/src/ui/components.md). It opens this embed with the signed-in user named and closes it when the app's sign-in changes. The rest of this section is for a host that builds the iframe itself.

The iframe can be shown whether or not the user is signed in. Create it when “More apps” is opened, naming the signed-in user in its fragment, and remove it on close so reopening loads current apps and preferences. Remove it when the host's own sign-in changes too, such as on sign-out, so an open embed doesn't keep the previous user. The host owns the modal and close control; there is no parent messaging API and no app list or credential is sent to it. The app browser fills its iframe and responds to its size. It shows a spinner until its apps load.

This example assumes puterOrigin comes from the host app's deployment settings. Translate the button and frame labels using the host app's localization system.

<button id="more-apps" type="button">More apps</button>
<dialog id="apps-dialog" aria-label="More apps">
  <button id="close-apps" type="button">Close</button>
  <div id="apps-frame"></div>
</dialog>
<style>
  #apps-dialog { padding: 12px; width: min(900px, 90vw); }
  #apps-frame iframe { width: 100%; height: min(650px, 75vh); border: 0; }
</style>
<script type="module">
  const puterOrigin = 'https://puter.com'; // Use this deployment's configured origin.
  const dialog = document.querySelector('#apps-dialog');
  const container = document.querySelector('#apps-frame');
  const trigger = document.querySelector('#more-apps');

  trigger.addEventListener('click', async () => {
    const url = new URL('/embed/apps', puterOrigin);
    // Signed out, the embed shows the recommended apps.
    const user = puter.auth.isSignedIn() ? await puter.auth.getUser().catch(() => null) : null;
    if (user) url.hash = `user=${encodeURIComponent(user.uuid)}`;
    const frame = document.createElement('iframe');
    frame.title = 'Your apps';
    frame.src = url.href;
    container.replaceChildren(frame);
    dialog.showModal();
  });
  document.querySelector('#close-apps').addEventListener('click', () => dialog.close());
  dialog.addEventListener('close', () => {
    container.replaceChildren();
    trigger.focus();
  });
</script>

Implementation and checks

createAppBrowser in src/gui/src/UI/Dashboard/AppBrowser.js owns the shared UI and data behavior; its guestApps option is the signed-out list. TabApps.js injects desktop actions. appsEmbed.js supplies new-tab launching through launchAppInTab (signed in) and guestAppLaunchUrl (signed out) in src/gui/src/helpers/appTabLaunch.js, which build the same app URL as launch_app, and loads only the embed entry, its SDK, and shared styles. embedHostUser in src/gui/src/helpers/embedHostUser.js reads the host's user from the fragment, and embedHostTheme.js beside it applies the host's theme. The normal GUI webpack build emits bundle.min.js and apps-embed.min.js. The /embed/apps route puts the recommended list in the page's config as guestApps. No puter.js API or app-list endpoint changes are needed.

Run the GUI component/browser tests (with the SDK bundle built and Playwright Chromium installed), and the backend route tests:

npx vitest run --config src/gui/vitest.config.js src/gui/src/UI/Dashboard/AppBrowser
npx vitest run --config src/backend/vitest.config.ts src/backend/server.test.ts