gitoriaLog in with ident

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf12fa1bcf12fa1bcState of 2026-09-27, before the move to gitoriamref12fa1bc/components/main.hl

7.0 KB

  1. // components/main.hl — THE SHELL: the application header (brand, All tickets, Inbox with
  2. // its live count, and TOP RIGHT the login: ident's identity selector + "Log in with ident",
  3. // or who you are + Log out), the display-name prompt of a first login, and the slot every
  4. // page renders into. All CSS is in styles.hl.
  5. //
  6. // LOGIN (ticket #7, CONCEPT.md "Login via ident"; ident README "How apps use ident"):
  7. // * THE SELECTOR: `<ident-selector key=IDENT_API_KEY>` from <ident>/selector.js. Choosing an
  8. // identity fires `ident-login` with a one-time code. hl:webex cannot listen to a custom DOM
  9. // event, so /login.js (plain JS) hands the code to the hidden input #identcode and fires its
  10. // `change` — the handler below sends it to the face `identLogin`, which exchanges it SERVER
  11. // SIDE and logs this session in. No reload: the face answers who you are and pushes
  12. // `signedIn` to this session's tabs (the pages show their forms).
  13. // * THE HOST TELLS THE SELECTOR: the element's class is `in` / `out` (a member); /login.js
  14. // mirrors it into the selector's `loggedIn` (the `logged-in` attribute). Log out → `out`
  15. // → the selector is reset.
  16. // * THE LOGIN BUTTON: a plain link to <ident>/login?key=&return=<TICKETS_PUBLIC_URL>/login/callback
  17. // (project.hl loginCallback). /login.js adds `?next=<this page>` to the return URL at the
  18. // click, so the user comes back to the page the login started from (ticket #10).
  19. // * /login.js RUNS AGAIN whenever this View flips between logged in / out (hl:webex
  20. // re-creates the header and the <script> elements; ticket #9): it installs itself once
  21. // per document and hands each one-time code to #identcode only once.
  22. // * FIRST LOGIN: no display name yet → the name prompt below; writing waits for it.
  23. import { inboxCountOf } from '../store.hl'
  24. import { siteName } from '../project.hl'
  25. import { identKey, selectorScript, loginHref, infoOfSession, exchangeCode, ensureUser, userInfo, setUserName, userOfSession } from '../users.hl'
  26. import { randomBytes } from 'hl:crypto'
  27. slot = null
  28. session = null
  29. // the inbox is PERSONAL (ticket #20): the tickets assigned to this session's user in pending and review
  30. inbox = inboxCountOf(userOfSession(session))
  31. hasInbox = inbox > 0
  32. // who this session is: { name, named, creator } — never the identity id
  33. me = infoOfSession(session)
  34. loggedIn = me != null
  35. loggedOut = me == null
  36. needsName = me != null && !me.named
  37. myName = me != null && me.named ? me.name : 'you'
  38. selectorClass = me != null ? 'in' : 'out'
  39. selectorKey = identKey
  40. identScript = selectorScript
  41. buttonHref = loginHref
  42. loginError = ''
  43. hasLoginError = false
  44. nameDraft = ''
  45. nameError = ''
  46. View {
  47. body {
  48. applicationHeader {
  49. a { class = "brand" href = "/" siteName }
  50. nav {
  51. a { id = "navall" href = "/" "All tickets" }
  52. a { id = "navinbox" href = "/inbox" "Inbox"
  53. if (hasInbox) { inboxBadge { id = "inboxcount" inbox } }
  54. }
  55. }
  56. userBox { id = "userbox"
  57. identSelector { id = "selector" key = selectorKey class = selectorClass }
  58. if (loggedIn) {
  59. a { id = "whoami" href = "/you" myName }
  60. button { id = "logout" type = "button" class = "quiet" "Log out" on click(e) { emit doLogout(e) } }
  61. }
  62. if (loggedOut) {
  63. a { id = "loginbutton" class = "button" href = buttonHref "Log in with ident" }
  64. }
  65. input { id = "identcode" type = "hidden" on change(e) { emit gotCode(e) } }
  66. }
  67. }
  68. if (hasLoginError) { p { id = "loginerror" class = "message banner" loginError } }
  69. if (needsName) {
  70. namePrompt {
  71. form { id = "nameform"
  72. on submit(e) {
  73. e.preventDefault()
  74. emit saveName(e)
  75. }
  76. label { "Welcome! Choose the display name others see as the author of what you write"
  77. input { id = "displayname" name = "displayname" required = "required" maxlength = "60" value = nameDraft on input(e) { emit setNameDraft(e.target.value) } }
  78. }
  79. button { id = "namesave" type = "submit" "Save" }
  80. p { id = "nameerror" class = "message" nameError }
  81. }
  82. }
  83. }
  84. main { slot }
  85. script { src = identScript }
  86. script { src = "/login.js" }
  87. // ticket #12: <md-editor> around every Markdown textarea (a page without JS keeps the plain
  88. // textarea; the script defines the element once, a re-run by webex is a no-op)
  89. script { src = "/md-editor.js" }
  90. }
  91. }
  92. // ---- the session as the page sees it ------------------------------------------------------
  93. showMe = (info) => {
  94. loggedIn = true
  95. loggedOut = false
  96. needsName = !info.named
  97. myName = info.named ? info.name : 'you'
  98. selectorClass = 'in'
  99. loginError = ''
  100. hasLoginError = false
  101. }
  102. showOut = () => {
  103. loggedIn = false
  104. loggedOut = true
  105. needsName = false
  106. myName = 'you'
  107. selectorClass = 'out'
  108. inbox = 0
  109. hasInbox = false
  110. }
  111. // the selector's code (via /login.js and the hidden input) → the server exchanges it
  112. on gotCode(e) {
  113. let code = e.target.value
  114. e.target.value = ''
  115. if (code == null || code == '') { return null }
  116. let r = emit server identLogin(code)
  117. if (r == null || r.error != null) {
  118. loginError = r != null ? r.error : 'the login failed'
  119. hasLoginError = true
  120. return null
  121. }
  122. showMe(r)
  123. emit refreshInbox()
  124. }
  125. on setNameDraft(v) { nameDraft = v }
  126. on saveName(e) {
  127. let r = emit server saveDisplayName(nameDraft)
  128. if (r == null || r.error != null) {
  129. nameError = r != null ? r.error : 'could not save the name'
  130. return null
  131. }
  132. nameError = ''
  133. showMe(r)
  134. }
  135. on doLogout(e) {
  136. emit server logOut()
  137. showOut()
  138. }
  139. // the inbox is personal: a ticket event asks the server how many tickets are assigned to this session now
  140. on refreshInbox() {
  141. let r = emit server myInboxCount()
  142. if (r != null && r.count != null) {
  143. inbox = r.count
  144. hasInbox = r.count > 0
  145. }
  146. }
  147. on client ticketCreated(row) { emit refreshInbox() }
  148. on client ticketEvent(ticketId, ev, row) { emit refreshInbox() }
  149. // this session's other tabs follow a login / name / logout
  150. on client signedIn(tag, info) {
  151. showMe(info)
  152. emit refreshInbox()
  153. }
  154. on client signedOut(tag) { showOut() }
  155. // ---- the faces (#31: hl:web fills `session` itself and refuses a trailing argument, hybriel#16) --
  156. on server identLogin(code, session) {
  157. if (session == null) { return { error = 'no session — reload the page' } }
  158. let x = exchangeCode(code)
  159. if (x.error != null) { return { error = x.error } }
  160. let u = ensureUser(x.identity)
  161. if (u == null) { return { error = 'could not store the user' } }
  162. let tag = randomBytes(16)
  163. session.user = { id = u.id }
  164. session.data.tag = tag
  165. let info = userInfo(u)
  166. emit client signedIn(tag, info)
  167. return info
  168. }
  169. on server saveDisplayName(name, session) {
  170. let u = userOfSession(session)
  171. if (u == null) { return { error = 'log in with ident first' } }
  172. let r = setUserName(u.id, name)
  173. if (r.error != null) { return { error = r.error } }
  174. let info = userInfo(r.user)
  175. emit client signedIn(session.data.tag, info)
  176. return info
  177. }
  178. on server myInboxCount(session) {
  179. return { count = inboxCountOf(userOfSession(session)) }
  180. }
  181. on server logOut(session) {
  182. if (session == null) { return { error = 'no session' } }
  183. // the tag stays (the push may fan out after this face returns); the next login replaces it
  184. emit client signedOut(session.data.tag)
  185. session.user = null
  186. return { ok = true }
  187. }

Branches

Latest commits

  • f12fa1bcState of 2026-09-27, before the move to gitoriamre