gitoriaLog in with ident

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9b27cb269b27cb26gitoria#21: installable app (manifest, service worker, offline start page), own iconmre9b27cb26/login.js

3.1 KB

  1. // login.js — the small bridge between ident's <ident-selector> and gitoria's shell (components/main.hl;
  2. // gitoria#14, the same as tickets' login.js). Plain JS because hl:web cannot listen to a custom DOM event:
  3. // `ident-login` (event.detail.code, the one-time code) goes into the hidden input #identcode, whose `change`
  4. // the shell handles (→ face gitoriaLogin, server-side exchange). The shell keeps the selector element's class
  5. // at `in` / `out`; it is mirrored here into the selector's `loggedIn` (false = back to "choose ident").
  6. // ident answers the selector only for a REGISTERED origin — the main address. On a repo address the shell gives
  7. // the selector the class `onrepo` (server-side, from the request's host) and styles.hl hides it; "Log in with
  8. // ident" stays the way in on every address.
  9. // BACK TO THE PAGE: the "Log in with ident" click gets `?next=<this page>` in its return URL — a path on the main
  10. // address, the full URL on a repo address (project.hl safeNext checks both). At the click, so it is the page the
  11. // address bar shows after any hl:web navigation.
  12. // RUNS ONCE PER DOCUMENT: hl:web re-creates the header and its <script>s when the login state flips, the
  13. // selector element is then a NEW one; listeners sit on `document`, the CURRENT #selector is looked up each
  14. // time, and a one-time code is handed to the shell at most once.
  15. (() => {
  16. if (window.__gitoriaLogin) { window.__gitoriaLogin.sync(); return; }
  17. const sync = () => {
  18. const sel = document.getElementById('selector');
  19. // not upgraded yet: a property set now would shadow the element's own accessor
  20. if (!sel || !window.customElements || !customElements.get('ident-selector')) return;
  21. const want = sel.classList.contains('in');
  22. if (sel.loggedIn !== want) sel.loggedIn = want;
  23. };
  24. new MutationObserver(sync).observe(document.documentElement, { subtree: true, childList: true, attributes: true, attributeFilter: ['class'] });
  25. if (window.customElements) customElements.whenDefined('ident-selector').then(sync);
  26. const handed = new Set();
  27. document.addEventListener('ident-login', (e) => {
  28. const code = e.detail && e.detail.code;
  29. const input = document.getElementById('identcode');
  30. if (!input || !code || handed.has(code)) return; // a one-time code is exchanged once
  31. handed.add(code);
  32. input.value = code;
  33. input.dispatchEvent(new Event('change', { bubbles: true }));
  34. });
  35. const withNext = (a) => {
  36. try {
  37. const u = new URL(a.href);
  38. const ret = new URL(u.searchParams.get('return'));
  39. const main = ret.host === location.host;
  40. const here = main ? location.pathname + location.search : location.href;
  41. ret.search = '';
  42. if (here !== '/' && here.length <= 500) ret.searchParams.set('next', here);
  43. u.searchParams.set('return', ret.href);
  44. a.href = u.href;
  45. } catch (err) { /* keep the server's href: the login still works, it returns to / */ }
  46. };
  47. const onButton = (e) => {
  48. const a = e.target && e.target.closest && e.target.closest('#loginbutton');
  49. if (a) withNext(a);
  50. };
  51. for (const t of ['click', 'auxclick', 'contextmenu']) document.addEventListener(t, onButton, true);
  52. window.__gitoriaLogin = { sync };
  53. })();

Branches

Latest commits

  • 9b27cb26gitoria#21: installable app (manifest, service worker, offline start page), own iconmre
  • 68dcb603deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • e2deed6dgitoria#20: "Add code" only on the Code page of an empty repository, no collapsiblemre
  • 8bb97ffddeploy.sh: never send .git or .gitignore to Byrodinmre
  • fd981932State of 2026-09-27; bin/ no longer tracked (Hybriel commit is in README)mre
  • 4a2d7125initial commitmre