gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit59e8fcc159e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre59e8fcc1/components/home.hl

14.0 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, createIdentity, updateIdentity } 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 ChooseIdentity from './chooseidentity.hl'
  25. import IdentityList from './identitylist.hl'
  26. import TimeZone from './timezone.hl'
  27. import { siteName } from '../project.hl'
  28. session = null
  29. rid = null
  30. // 'code' on THE CODE PAGE `/code` and `/signin/<rid>/code` (ident#20; lib/api.hl codePage
  31. // renders this component there, only while the session has a pending code), else null
  32. step = null
  33. onCodePage = step == 'code'
  34. // the pending address, read by codePage from the session and handed in as a param (mission
  35. // 048): read here from the session, hl:web (64527baa) would re-derive it after EVERY face
  36. // that takes the session — after the 5th wrong code it turned null and the code form
  37. // vanished with nothing to click (gate "after 5 wrong tries even the RIGHT code is refused")
  38. pending = null
  39. // where this login lives: the email form, and the code page after "Send me a code"
  40. homeUrl = rid != null ? '/signin/' + rid : '/'
  41. codeUrl = rid != null ? '/signin/' + rid + '/code' : '/code'
  42. me = accountOfSession(&session)
  43. signedIn = me != null
  44. // THE APP'S LOGIN REQUEST (null on `/`, or when unknown / expired)
  45. request = rid != null ? requestOf(rid) : null
  46. forApp = request != null
  47. badRequest = rid != null && request == null
  48. appName = request != null ? request.app.name : ''
  49. appOrigin = request != null ? request.origin : ''
  50. forInvite = request != null && request.invite != ''
  51. requestLead = forInvite ? 'You are invited to ' : 'Sign in to '
  52. onWelcome = step == 'welcome' && me != null && forApp // a NEW address sent here from the selector's frame (ident#31): the default identity's form first
  53. choosing = forApp && me != null && !onWelcome // the identity choice for the app
  54. manage = rid == null && me != null // identities + time zone (only on `/`)
  55. meEmail = me != null ? me.email : ''
  56. timeZone = me != null ? me.timeZone : ''
  57. identities = me != null ? identityRows(me.id) : []
  58. __title = siteName + (forApp ? ' | sign in to ' + appName : (me != null ? ' | your identities' : ' | sign in'))
  59. // the login steps (a View's `if` takes a member, so each step is one).
  60. // THE CODE STEP IS ITS OWN PAGE (ident#20, creator: "just make a /code where it checks a
  61. // pending code"): "Send me a code" records the address in this browser's session (face
  62. // rememberPending) and goes to `/code`; that page shows the code form for the session's
  63. // pending address (lib/login.hl pendingOf) — so a reload, a second tab or a re-seed keeps it.
  64. pendingEmail = onCodePage && me == null && !badRequest ? pending : null
  65. askEmail = me == null && !badRequest && !onCodePage
  66. askCode = pendingEmail != null
  67. sentTo = pendingEmail != null ? pendingEmail : ''
  68. message = ''
  69. notice = ''
  70. // THE IDENTITY FORM — new, edit, and right after the first login the default identity
  71. // ("welcome": the names are optional, Skip closes it)
  72. editing = onWelcome
  73. welcome = onWelcome
  74. notWelcome = !onWelcome // `if (!welcome)` in a View renders nothing: an `if` takes a member
  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. avatarPreviewClass = hasAvatarPreview ? 'avatar' : 'avatar hidden'
  87. avatarRemoveClass = hasAvatarPreview ? 'quiet small' : 'quiet small hidden'
  88. View {
  89. identCard {
  90. if (forApp) {
  91. appRequest { id = "apprequest" requestLead strong { id = "appname" appName } " " requestOrigin { id = "apporigin" appOrigin } }
  92. }
  93. if (badRequest) {
  94. h1 { "Login request expired" }
  95. p { id = "badrequest" class = "message" "This login request is unknown or expired. Go back to the app and start the login again." }
  96. }
  97. if (askEmail) {
  98. h1 { "Sign in" }
  99. 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." }
  100. EmailStep {}
  101. }
  102. if (askCode) {
  103. h1 { "Enter your code" }
  104. CodeStep { sentTo = sentTo tail = ". It is valid for 10 minutes." }
  105. }
  106. if (signedIn) {
  107. if (editing) {
  108. form { id = "identityform"
  109. on submit(e) {
  110. e.preventDefault()
  111. emit saveForm(e)
  112. }
  113. h1 { id = "formheading" editHeading }
  114. if (welcome) {
  115. p { id = "welcome" class = "lead" "Your account is ready and has a default identity. All of these fields are " strong { "optional" } " — fill in what you like, or skip it and do it later." }
  116. }
  117. label { "Identity name " fieldHint { "(shown when you pick an identity in an app; optional)" }
  118. input { id = "fidentityname" name = "identityName" maxlength = "60" autocomplete = "off" value = fIdentityName on input(e) { emit setF('identityName', e.target.value) } }
  119. }
  120. label { "Nickname " fieldHint { "(optional)" }
  121. input { id = "fnickname" name = "nickname" maxlength = "60" autocomplete = "nickname" value = fNickname on input(e) { emit setF('nickname', e.target.value) } }
  122. }
  123. label { "First name " fieldHint { "(optional)" }
  124. input { id = "ffirstname" name = "firstname" maxlength = "60" autocomplete = "given-name" value = fFirstname on input(e) { emit setF('firstname', e.target.value) } }
  125. }
  126. label { "Last name " fieldHint { "(optional)" }
  127. input { id = "flastname" name = "lastname" maxlength = "60" autocomplete = "family-name" value = fLastname on input(e) { emit setF('lastname', e.target.value) } }
  128. }
  129. avatarField {
  130. img { id = "avatarpreview" class = avatarPreviewClass src = fAvatar alt = "" width = "48" height = "48" }
  131. label { "Avatar " fieldHint { "(optional; a picture apps can show, e.g. next to your comments)" }
  132. input { id = "favatarfile" type = "file" accept = "image/png,image/jpeg,image/webp,image/gif" }
  133. }
  134. input { id = "favatar" name = "avatar" type = "hidden" value = fAvatar on input(e) { emit setF('avatar', e.target.value) } }
  135. button { id = "avatarremove" type = "button" class = avatarRemoveClass "Remove picture" on click(e) { emit setF('avatar', '') } }
  136. span { id = "avatarnote" class = "avatar-note" }
  137. }
  138. formButtons {
  139. button { id = "saveidentity" type = "submit" "Save" }
  140. if (welcome) { button { id = "skip" type = "button" class = "quiet" "Skip" on click(e) { emit closeForm(e) } } }
  141. if (notWelcome) { button { id = "cancel" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } } }
  142. }
  143. }
  144. }
  145. if (choosing) {
  146. ChooseIdentity { rid = rid appName = appName identities = identities }
  147. }
  148. if (manage) {
  149. IdentityList { identities = identities }
  150. TimeZone { timeZone = timeZone }
  151. }
  152. }
  153. p { id = "notice" class = "notice" notice }
  154. p { id = "message" class = "message" message }
  155. }
  156. }
  157. on setF(name, v) {
  158. if (name == 'identityName') { fIdentityName = v }
  159. if (name == 'nickname') { fNickname = v }
  160. if (name == 'firstname') { fFirstname = v }
  161. if (name == 'lastname') { fLastname = v }
  162. if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }
  163. }
  164. // 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.
  165. // THE SESSION REMEMBERS THE STEP, then the browser goes to the code page. A function route gets no session (hybriel#11), so the
  166. // face records it; it refuses unless a code for the address is really waiting.
  167. emailSent = (String address) => {
  168. k = emit server rememberPending(address)
  169. if (k == null) {
  170. message = 'the server did not answer — try again'
  171. return null
  172. }
  173. if (k.error != null) {
  174. message = k.error
  175. return null
  176. }
  177. navigate(codeUrl)
  178. }
  179. // the code step (components/codestep.hl) checked the code: why not, or the signed-in account
  180. codeFailed = (String text) => { message = text }
  181. codeVerified = (r) => {
  182. askCode = false
  183. // signed in: the address bar leaves the code page (a reload of /code would go back
  184. // to it anyway — lib/api.hl codePage sends a signed-in browser there)
  185. window.history.replaceState(null, '', homeUrl)
  186. meEmail = r.account.email
  187. timeZone = r.account.timeZone
  188. identities = r.identities
  189. signedIn = true
  190. notice = ''
  191. manage = !forApp
  192. choosing = forApp
  193. // the shell (components/main.hl) shows the user menu from the account: a page's emit reaches every level above it
  194. // (hybriel#193), so the header follows the in-place sign-in without a reload — the welcome form below stays in place
  195. emit shellSession(r.account.email)
  196. // THE FIRST LOGIN: the default identity's names, optional (for an app too: the
  197. // choice follows once the form is saved or skipped)
  198. if (r.created) {
  199. fillForm(r.identities[0])
  200. editHeading = 'Welcome — name your default identity'
  201. welcome = true
  202. notWelcome = false
  203. editing = true
  204. choosing = false
  205. }
  206. }
  207. fillForm = (row) => {
  208. editId = row.id
  209. fIdentityName = row.identityName
  210. fNickname = row.nickname
  211. fFirstname = row.firstname
  212. fLastname = row.lastname
  213. fAvatar = row.avatar
  214. hasAvatarPreview = row.hasAvatar
  215. return null
  216. }
  217. rowOf = (id) => {
  218. for (row of identities) { if ('' + row.id == '' + id) { return row } }
  219. return null
  220. }
  221. openNew = () => {
  222. message = ''
  223. notice = ''
  224. editId = ''
  225. fIdentityName = ''
  226. fNickname = ''
  227. fFirstname = ''
  228. fLastname = ''
  229. fAvatar = ''
  230. hasAvatarPreview = false
  231. editHeading = 'New identity'
  232. welcome = false
  233. notWelcome = true
  234. editing = true
  235. }
  236. openEdit = (id) => {
  237. message = ''
  238. notice = ''
  239. row = rowOf(id)
  240. if (row == null) { return null }
  241. fillForm(row)
  242. editHeading = 'Edit ' + row.label
  243. welcome = false
  244. notWelcome = true
  245. editing = true
  246. }
  247. on closeForm(e) {
  248. editing = false
  249. welcome = false
  250. notWelcome = true
  251. message = ''
  252. choosing = forApp
  253. }
  254. on saveForm(e) {
  255. message = ''
  256. fields = { identityName = fIdentityName nickname = fNickname firstname = fFirstname lastname = fLastname avatar = fAvatar }
  257. let r = null
  258. if (editId == '' && welcome) {
  259. r = emit server editIdentity('' + welcomeId, fields)
  260. } else if (editId == '') {
  261. r = emit server addIdentity(fields)
  262. } else {
  263. r = emit server editIdentity('' + editId, fields)
  264. }
  265. if (r == null) {
  266. message = 'the server did not answer — try again'
  267. return null
  268. }
  269. if (r.error != null) {
  270. message = r.error
  271. return null
  272. }
  273. identities = r.identities
  274. notice = editId == '' && !welcome ? 'Identity created.' : 'Identity saved.'
  275. editing = false
  276. welcome = false
  277. notWelcome = true
  278. choosing = forApp
  279. }
  280. // the identity list (components/identitylist.hl) deleted an identity
  281. identitiesDeleted = (rows, String id) => {
  282. identities = rows
  283. if ('' + editId == id) { editing = false }
  284. notice = 'Identity deleted.'
  285. }
  286. // the time zone component (components/timezone.hl) saved the account's zone
  287. zoneSaved = (String zone) => {
  288. timeZone = zone
  289. notice = 'Time zone saved.'
  290. }
  291. // a placed component shows its answer here (the #message and #notice lines under the card)
  292. status = (String text, String note) => {
  293. message = text
  294. notice = note
  295. }
  296. // "Other address": the session forgets the pending sign-in, back to the email form
  297. back = () => {
  298. message = ''
  299. r = emit server forgetPending()
  300. navigate(homeUrl)
  301. }
  302. // ---- the faces ------------------------------------------------------------------------
  303. // A face's LAST parameter is the session (the framework appends it). Every face that
  304. // needs the account asks accountOfSession, which only accepts the framework's Session.
  305. // A face runs on a BLANK instance: it has its arguments and its imports, not this
  306. // component's members or functions.
  307. // (the code request is NOT a face since mission 010: POST /api/code in lib/api.hl — a face
  308. // sees no request headers, so it could not apply the per-IP limit; a face here would be a
  309. // way around it)
  310. on server addIdentity(fields, session) {
  311. let me = accountOfSession(&session)
  312. if (me == null) { return { error = 'you are not signed in' } }
  313. r = createIdentity(me.id, fields)
  314. if (r.error != null) { return r }
  315. return { identity = r.identity identities = identityRows(me.id) }
  316. }
  317. on server editIdentity(id, fields, session) {
  318. let me = accountOfSession(&session)
  319. if (me == null) { return { error = 'you are not signed in' } }
  320. r = updateIdentity(me.id, id, fields)
  321. if (r.error != null) { return r }
  322. return { identity = r.identity identities = identityRows(me.id) }
  323. }
  324. // THE PENDING SIGN-IN (ident#20): recorded after POST /api/code answered, only while a
  325. // code for that address is really waiting (lib/login.hl recordPending); "Other address"
  326. // forgets it. Named apart from the store functions (faces dispatch by name, hybriel#36).
  327. on server rememberPending(email, session) {
  328. return recordPending(&session, email)
  329. }
  330. on server forgetPending(session) {
  331. return dropPending(&session)
  332. }

Branches

Latest commits

  • 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
  • 23971fb7ident#49: media queries in the language's @media formmre
  • 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre
  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre