OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-note / lib / identity-ui.js

33 lines · javascript ref:
/**
 * Identity UI — inject browser identity script into SSR pages
 *
 * Loads the existing web/js/identity.js which:
 * - Generates/stores ed25519 keypair in localStorage
 * - Intercepts fetch() to inject challenge-response auth headers
 * - Provides window.HnIdentity API
 *
 * The script must load BEFORE any fetch() calls, so it's placed at the
 * top of <body> (blocking). This matches pear-git's pattern.
 *
 * @param {string} pathPrefix - Server path prefix (e.g., '/hypernote')
 * @returns {string} HTML script tags
 */
export function renderIdentityScript (pathPrefix = '') {
  const base = pathPrefix || ''

  // Set up HnApi.basePath before identity.js loads (it reads window.HnApi)
  // Also provide identityReady promise for dependent code (nav admin check)
  return `<script>
window.HnApi = { basePath: ${JSON.stringify(base)}, getApiBase: function () { return ${JSON.stringify(base)}; } };
window._hnIdentityResolve = null;
window.identityReady = new Promise(function (resolve) { window._hnIdentityResolve = resolve; });
</script>
<script src="${base}/web/js/identity.js"></script>
<script>
(function () {
  // Signal that identity module has loaded
  if (window._hnIdentityResolve) window._hnIdentityResolve();
})();
</script>`
}