gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchf18b-shell-session19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmref18b-shell-session/components/home.hl

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

Branches

Latest commits

  • 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
  • 5d1b3fedMerge branch 'v61-day61' into mainIdent
  • 83faeb15|||PR ident: vendor Hybriel day61mre
  • b2572b34Merge branch 'v59-day59' into mainIdent
  • 67cf016d|||PR ident: vendor Hybriel day59mre
  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre