gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit47e516af47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre47e516af/components/home.hl

10.6 KB

  1. // components/home.hl — `/` and `/signin/:rid`: ALL OF IDENT (pieces 1+2, tickets #24 #25;
  2. // CONCEPT.md).
  3. // Signed out: email → a six-digit code by mail → signed in. There is no registration:
  4. // the first right code for an address creates the account with a DEFAULT IDENTITY, then
  5. // this page shows that identity's name fields and says they are optional (Skip).
  6. // Signed in: the account's identities (list, new, edit, delete — never the last one)
  7. // and its time zone (the browser's at the first login; changeable here).
  8. //
  9. // Under `/signin/<rid>` the page belongs to an APP'S LOGIN BUTTON (lib/apps.hl requestOf):
  10. // it names the app, signs in to ident if needed, then asks WHICH IDENTITY the app gets
  11. // (with a single identity it still shows which one, one click) and sends the browser back
  12. // to the app with a one-time code. Managing identities stays on `/`.
  13. //
  14. // Every step takes the server's answer from the CALL (the value form of emit): it rides
  15. // the ack, over the socket or the POST fallback.
  16. parent './main.hl'
  17. import { accountOfSession } from '../lib/accounts.hl'
  18. import { recordPending, dropPending } from '../lib/login.hl'
  19. import { identityRows } from '../lib/identities.hl'
  20. import { requestOf } from '../lib/apps.hl'
  21. import { navigate } from 'hl:web'
  22. import EmailStep from './emailstep.hl'
  23. import CodeStep from './codestep.hl'
  24. import IdentityForm from './identityform.hl'
  25. import ChooseIdentity from './chooseidentity.hl'
  26. import IdentityList from './identitylist.hl'
  27. import TimeZone from './timezone.hl'
  28. import { siteName } from '../project.hl'
  29. session = null
  30. rid = null
  31. // 'code' on THE CODE PAGE `/code` and `/signin/<rid>/code` (ident#20; lib/api.hl codePage
  32. // renders this component there, only while the session has a pending code), else null
  33. step = null
  34. onCodePage = step == 'code'
  35. // the pending address, read by codePage from the session and handed in as a param (mission
  36. // 048): read here from the session, hl:web (64527baa) would re-derive it after EVERY face
  37. // that takes the session — after the 5th wrong code it turned null and the code form
  38. // vanished with nothing to click (gate "after 5 wrong tries even the RIGHT code is refused")
  39. pending = null
  40. // where this login lives: the email form, and the code page after "Send me a code"
  41. homeUrl = rid != null ? '/signin/' + rid : '/'
  42. codeUrl = rid != null ? '/signin/' + rid + '/code' : '/code'
  43. me = accountOfSession(&session)
  44. signedIn = me != null
  45. // THE APP'S LOGIN REQUEST (null on `/`, or when unknown / expired)
  46. request = rid != null ? requestOf(rid) : null
  47. forApp = request != null
  48. badRequest = rid != null && request == null
  49. appName = request != null ? request.app.name : ''
  50. appOrigin = request != null ? request.origin : ''
  51. forInvite = request != null && request.invite != ''
  52. requestLead = forInvite ? 'You are invited to ' : 'Sign in to '
  53. onWelcome = step == 'welcome' && me != null && forApp // a NEW address sent here from the selector's frame (ident#31): the default identity's form first
  54. choosing = forApp && me != null && !onWelcome // the identity choice for the app
  55. manage = rid == null && me != null // identities + time zone (only on `/`)
  56. meEmail = me != null ? me.email : ''
  57. timeZone = me != null ? me.timeZone : ''
  58. identities = me != null ? identityRows(me.id) : []
  59. __title = siteName + (forApp ? ' | sign in to ' + appName : (me != null ? ' | your identities' : ' | sign in'))
  60. // the login steps (a View's `if` takes a member, so each step is one).
  61. // THE CODE STEP IS ITS OWN PAGE (ident#20, creator: "just make a /code where it checks a
  62. // pending code"): "Send me a code" records the address in this browser's session (face
  63. // rememberPending) and goes to `/code`; that page shows the code form for the session's
  64. // pending address (lib/login.hl pendingOf) — so a reload, a second tab or a re-seed keeps it.
  65. pendingEmail = onCodePage && me == null && !badRequest ? pending : null
  66. askEmail = me == null && !badRequest && !onCodePage
  67. askCode = pendingEmail != null
  68. sentTo = pendingEmail != null ? pendingEmail : ''
  69. message = ''
  70. notice = ''
  71. // THE IDENTITY FORM — new, edit, and right after the first login the default identity
  72. // ("welcome": the names are optional, Skip closes it)
  73. editing = onWelcome
  74. welcome = onWelcome
  75. editId = '' // '' = a new identity, else the identity's id (a UUID)
  76. // the default identity the welcome form (/signin/<rid>/welcome) edits. It is its OWN member: `editId` must stay a plain value (a member made of an
  77. // expression is recomputed when `identities` changes, which reset the id of every identity edited on `/`)
  78. welcomeId = onWelcome ? '' + identities[0].id : ''
  79. editHeading = onWelcome ? 'Welcome — name your default identity' : ''
  80. fIdentityName = ''
  81. fNickname = ''
  82. fFirstname = ''
  83. fLastname = ''
  84. fAvatar = ''
  85. hasAvatarPreview = false
  86. View {
  87. identCard {
  88. if (forApp) {
  89. appRequest { id = "apprequest" requestLead strong { id = "appname" appName } " " requestOrigin { id = "apporigin" appOrigin } }
  90. }
  91. if (badRequest) {
  92. h1 { "Login request expired" }
  93. p { id = "badrequest" class = "message" "This login request is unknown or expired. Go back to the app and start the login again." }
  94. }
  95. if (askEmail) {
  96. h1 { "Sign in" }
  97. p { class = "lead" "We mail you a one-time code. There are no passwords, and no sign-up: the first login with an address creates its account." }
  98. EmailStep {}
  99. }
  100. if (askCode) {
  101. h1 { "Enter your code" }
  102. CodeStep { sentTo = sentTo tail = ". It is valid for 10 minutes." }
  103. }
  104. if (signedIn) {
  105. if (editing) {
  106. IdentityForm { editHeading = editHeading welcome = welcome editId = editId welcomeId = welcomeId fIdentityName = fIdentityName fNickname = fNickname fFirstname = fFirstname fLastname = fLastname fAvatar = fAvatar hasAvatarPreview = hasAvatarPreview }
  107. }
  108. if (choosing) {
  109. ChooseIdentity { rid = rid appName = appName identities = identities }
  110. }
  111. if (manage) {
  112. IdentityList { identities = identities }
  113. TimeZone { timeZone = timeZone }
  114. }
  115. }
  116. p { id = "notice" class = "notice" notice }
  117. p { id = "message" class = "message" message }
  118. }
  119. }
  120. // a placed component shows its answer here (the #message and #notice lines under the card)
  121. status = (String text, String note) => {
  122. message = text
  123. notice = note
  124. }
  125. // the email step (components/emailstep.hl) asked for a code (a plain POST to /api/code, lib/api.hl) and one is on its way.
  126. // THE SESSION REMEMBERS THE STEP, then the browser goes to the code page. A function route gets no session (hybriel#11), so the
  127. // face records it; it refuses unless a code for the address is really waiting.
  128. emailSent = (String address) => {
  129. k = emit server rememberPending(address)
  130. if (k == null) {
  131. message = 'the server did not answer — try again'
  132. return null
  133. }
  134. if (k.error != null) {
  135. message = k.error
  136. return null
  137. }
  138. navigate(codeUrl)
  139. }
  140. // the code step (components/codestep.hl) checked the code: why not, or the signed-in account
  141. codeFailed = (String text) => { message = text }
  142. codeVerified = (r) => {
  143. askCode = false
  144. // signed in: the address bar leaves the code page (a reload of /code would go back
  145. // to it anyway — lib/api.hl codePage sends a signed-in browser there)
  146. window.history.replaceState(null, '', homeUrl)
  147. meEmail = r.account.email
  148. timeZone = r.account.timeZone
  149. identities = r.identities
  150. signedIn = true
  151. notice = ''
  152. manage = !forApp
  153. choosing = forApp
  154. // the shell (components/main.hl) shows the user menu from the account: a page's emit reaches every level above it
  155. // (hybriel#193), so the header follows the in-place sign-in without a reload — the welcome form below stays in place
  156. emit shellSession(r.account.email)
  157. // THE FIRST LOGIN: the default identity's names, optional (for an app too: the
  158. // choice follows once the form is saved or skipped)
  159. if (r.created) {
  160. fillForm(r.identities[0])
  161. editHeading = 'Welcome — name your default identity'
  162. welcome = true
  163. editing = true
  164. choosing = false
  165. }
  166. }
  167. // THE IDENTITY FORM (components/identityform.hl) is opened with the values to start from; what the user types is handed up by setF
  168. fillForm = (row) => {
  169. editId = row.id
  170. fIdentityName = row.identityName
  171. fNickname = row.nickname
  172. fFirstname = row.firstname
  173. fLastname = row.lastname
  174. fAvatar = row.avatar
  175. hasAvatarPreview = row.hasAvatar
  176. return null
  177. }
  178. rowOf = (id) => {
  179. for (row of identities) { if ('' + row.id == '' + id) { return row } }
  180. return null
  181. }
  182. openNew = () => {
  183. message = ''
  184. notice = ''
  185. editId = ''
  186. fIdentityName = ''
  187. fNickname = ''
  188. fFirstname = ''
  189. fLastname = ''
  190. fAvatar = ''
  191. hasAvatarPreview = false
  192. editHeading = 'New identity'
  193. welcome = false
  194. editing = true
  195. }
  196. openEdit = (id) => {
  197. message = ''
  198. notice = ''
  199. row = rowOf(id)
  200. if (row == null) { return null }
  201. fillForm(row)
  202. editHeading = 'Edit ' + row.label
  203. welcome = false
  204. editing = true
  205. }
  206. setF = (String name, String v) => {
  207. if (name == 'identityName') { fIdentityName = v }
  208. if (name == 'nickname') { fNickname = v }
  209. if (name == 'firstname') { fFirstname = v }
  210. if (name == 'lastname') { fLastname = v }
  211. if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }
  212. }
  213. closeForm = () => {
  214. editing = false
  215. welcome = false
  216. message = ''
  217. choosing = forApp
  218. }
  219. formSaved = (rows, String text) => {
  220. identities = rows
  221. notice = text
  222. editing = false
  223. welcome = false
  224. choosing = forApp
  225. }
  226. // the identity list (components/identitylist.hl) deleted an identity
  227. identitiesDeleted = (rows, String id) => {
  228. identities = rows
  229. if ('' + editId == id) { editing = false }
  230. notice = 'Identity deleted.'
  231. }
  232. // the time zone component (components/timezone.hl) saved the account's zone
  233. zoneSaved = (String zone) => {
  234. timeZone = zone
  235. notice = 'Time zone saved.'
  236. }
  237. // "Other address": the session forgets the pending sign-in, back to the email form
  238. back = () => {
  239. message = ''
  240. r = emit server forgetPending()
  241. navigate(homeUrl)
  242. }
  243. // ---- the faces ------------------------------------------------------------------------
  244. // A face's LAST parameter is the session (the framework appends it). Every face that
  245. // needs the account asks accountOfSession, which only accepts the framework's Session.
  246. // A face runs on a BLANK instance: it has its arguments and its imports, not this
  247. // component's members or functions.
  248. // (the code request is NOT a face since mission 010: POST /api/code in lib/api.hl — a face
  249. // sees no request headers, so it could not apply the per-IP limit; a face here would be a
  250. // way around it)
  251. // THE PENDING SIGN-IN (ident#20): recorded after POST /api/code answered, only while a
  252. // code for that address is really waiting (lib/login.hl recordPending); "Other address"
  253. // forgets it. Named apart from the store functions (faces dispatch by name, hybriel#36).
  254. on server rememberPending(email, session) {
  255. return recordPending(&session, email)
  256. }
  257. on server forgetPending(session) {
  258. return dropPending(&session)
  259. }

Branches

Latest commits

  • 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
  • 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
  • f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre
  • c512fb96Merge branch 'f4-homesplit' into mainIdent
  • 07d1717c|||PR ident#40 home.hl is split into components, one per area: email step, identity form, identity list, app identity choice, time zone; each moves its faces with it, the email step is shared with embed.hl (#40.3, rest of #40.2)mre
  • 59e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre
  • e23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmre
  • e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmre
  • 8cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre
  • 6fad3bd2Merge branch 'f3-codestep' into mainIdent
  • 51e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre
  • c28bddeeMerge branch 'f2-shell' into mainIdent
  • c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mre
  • 060f3870ident#40.4: use the shell sign-out faces in regression gatesmre
  • 9b94aa6aMerge branch 'f1b-cleanup' into mainIdent
  • edcfce3f|||PR ident#41 ident#43 ident#44 login error page as a component (same status codes and text, pixel-identical), declarations and route wiring cleaned upmre
  • 3ac85995ident#43 ident#44: plain declarations for never-reassigned lets, one envString helper, named route functions and audience rule out of project.hlmre
  • c7eef14aMerge branch 'nv1-navigate' into mainIdent
  • 644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre
  • 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre