gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite24b59a6e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmree24b59a6/components/home.hl

17.2 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, deleteIdentity } from '../lib/identities.hl'
  20. import { requestOf, grantRequest } from '../lib/apps.hl'
  21. import { navigate } from 'hl:web'
  22. import EmailStep from './emailstep.hl'
  23. import CodeStep from './codestep.hl'
  24. import TimeZone from './timezone.hl'
  25. import { siteName } from '../project.hl'
  26. session = null
  27. rid = null
  28. // 'code' on THE CODE PAGE `/code` and `/signin/<rid>/code` (ident#20; lib/api.hl codePage
  29. // renders this component there, only while the session has a pending code), else null
  30. step = null
  31. onCodePage = step == 'code'
  32. // the pending address, read by codePage from the session and handed in as a param (mission
  33. // 048): read here from the session, hl:web (64527baa) would re-derive it after EVERY face
  34. // that takes the session — after the 5th wrong code it turned null and the code form
  35. // vanished with nothing to click (gate "after 5 wrong tries even the RIGHT code is refused")
  36. pending = null
  37. // where this login lives: the email form, and the code page after "Send me a code"
  38. homeUrl = rid != null ? '/signin/' + rid : '/'
  39. codeUrl = rid != null ? '/signin/' + rid + '/code' : '/code'
  40. me = accountOfSession(&session)
  41. signedIn = me != null
  42. // THE APP'S LOGIN REQUEST (null on `/`, or when unknown / expired)
  43. request = rid != null ? requestOf(rid) : null
  44. forApp = request != null
  45. badRequest = rid != null && request == null
  46. appName = request != null ? request.app.name : ''
  47. appOrigin = request != null ? request.origin : ''
  48. forInvite = request != null && request.invite != ''
  49. requestLead = forInvite ? 'You are invited to ' : 'Sign in to '
  50. onWelcome = step == 'welcome' && me != null && forApp // a NEW address sent here from the selector's frame (ident#31): the default identity's form first
  51. choosing = forApp && me != null && !onWelcome // the identity choice for the app
  52. manage = rid == null && me != null // identities + time zone (only on `/`)
  53. meEmail = me != null ? me.email : ''
  54. timeZone = me != null ? me.timeZone : ''
  55. identities = me != null ? identityRows(me.id) : []
  56. __title = siteName + (forApp ? ' | sign in to ' + appName : (me != null ? ' | your identities' : ' | sign in'))
  57. // the login steps (a View's `if` takes a member, so each step is one).
  58. // THE CODE STEP IS ITS OWN PAGE (ident#20, creator: "just make a /code where it checks a
  59. // pending code"): "Send me a code" records the address in this browser's session (face
  60. // rememberPending) and goes to `/code`; that page shows the code form for the session's
  61. // pending address (lib/login.hl pendingOf) — so a reload, a second tab or a re-seed keeps it.
  62. pendingEmail = onCodePage && me == null && !badRequest ? pending : null
  63. askEmail = me == null && !badRequest && !onCodePage
  64. askCode = pendingEmail != null
  65. sentTo = pendingEmail != null ? pendingEmail : ''
  66. message = ''
  67. notice = ''
  68. // THE IDENTITY FORM — new, edit, and right after the first login the default identity
  69. // ("welcome": the names are optional, Skip closes it)
  70. editing = onWelcome
  71. welcome = onWelcome
  72. notWelcome = !onWelcome // `if (!welcome)` in a View renders nothing: an `if` takes a member
  73. editId = '' // '' = a new identity, else the identity's id (a UUID)
  74. // 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
  75. // expression is recomputed when `identities` changes, which reset the id of every identity edited on `/`)
  76. welcomeId = onWelcome ? '' + identities[0].id : ''
  77. editHeading = onWelcome ? 'Welcome — name your default identity' : ''
  78. fIdentityName = ''
  79. fNickname = ''
  80. fFirstname = ''
  81. fLastname = ''
  82. fAvatar = ''
  83. hasAvatarPreview = false
  84. avatarPreviewClass = hasAvatarPreview ? 'avatar' : 'avatar hidden'
  85. avatarRemoveClass = hasAvatarPreview ? 'quiet small' : 'quiet small hidden'
  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. form { id = "identityform"
  107. on submit(e) {
  108. e.preventDefault()
  109. emit saveForm(e)
  110. }
  111. h1 { id = "formheading" editHeading }
  112. if (welcome) {
  113. 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." }
  114. }
  115. label { "Identity name " fieldHint { "(shown when you pick an identity in an app; optional)" }
  116. input { id = "fidentityname" name = "identityName" maxlength = "60" autocomplete = "off" value = fIdentityName on input(e) { emit setF('identityName', e.target.value) } }
  117. }
  118. label { "Nickname " fieldHint { "(optional)" }
  119. input { id = "fnickname" name = "nickname" maxlength = "60" autocomplete = "nickname" value = fNickname on input(e) { emit setF('nickname', e.target.value) } }
  120. }
  121. label { "First name " fieldHint { "(optional)" }
  122. input { id = "ffirstname" name = "firstname" maxlength = "60" autocomplete = "given-name" value = fFirstname on input(e) { emit setF('firstname', e.target.value) } }
  123. }
  124. label { "Last name " fieldHint { "(optional)" }
  125. input { id = "flastname" name = "lastname" maxlength = "60" autocomplete = "family-name" value = fLastname on input(e) { emit setF('lastname', e.target.value) } }
  126. }
  127. avatarField {
  128. img { id = "avatarpreview" class = avatarPreviewClass src = fAvatar alt = "" width = "48" height = "48" }
  129. label { "Avatar " fieldHint { "(optional; a picture apps can show, e.g. next to your comments)" }
  130. input { id = "favatarfile" type = "file" accept = "image/png,image/jpeg,image/webp,image/gif" }
  131. }
  132. input { id = "favatar" name = "avatar" type = "hidden" value = fAvatar on input(e) { emit setF('avatar', e.target.value) } }
  133. button { id = "avatarremove" type = "button" class = avatarRemoveClass "Remove picture" on click(e) { emit setF('avatar', '') } }
  134. span { id = "avatarnote" class = "avatar-note" }
  135. }
  136. formButtons {
  137. button { id = "saveidentity" type = "submit" "Save" }
  138. if (welcome) { button { id = "skip" type = "button" class = "quiet" "Skip" on click(e) { emit closeForm(e) } } }
  139. if (notWelcome) { button { id = "cancel" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } } }
  140. }
  141. }
  142. }
  143. if (choosing) {
  144. section { id = "choosesection"
  145. h1 { "Choose an identity" }
  146. p { class = "lead" "Which identity signs in to " strong { appName } "? The app gets only the identity's short id — no email, no names." }
  147. ul { id = "chooselist"
  148. for (row of identities) {
  149. li { class = "choice"
  150. if (row.hasAvatar) { img { class = "avatar" src = row.avatar alt = "" width = "32" height = "32" } }
  151. identityMain {
  152. identityLabel { row.label }
  153. identityShortId { class = "shortid" row.shortId }
  154. if (row.isDefault) { defaultBadge { "default" } }
  155. identityDetails { row.details }
  156. }
  157. button { type = "button" class = "choose" value = row.id "Continue" on click(e) { emit choose(e.target.value) } }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. if (manage) {
  164. section { id = "identitiessection"
  165. sectionHead {
  166. h1 { "Your identities" }
  167. button { id = "newidentity" type = "button" class = "small" "New identity" on click(e) { emit openNew(e) } }
  168. }
  169. ul { id = "identities"
  170. for (row of identities) {
  171. li { class = "identity"
  172. if (row.hasAvatar) { img { class = "avatar" src = row.avatar alt = "" width = "32" height = "32" } }
  173. identityMain {
  174. identityLabel { row.label }
  175. identityShortId { class = "shortid" row.shortId }
  176. if (row.isDefault) { defaultBadge { "default" } }
  177. identityDetails { row.details }
  178. }
  179. identityActions {
  180. a { class = "used" href = row.usedHref "Used in…" }
  181. button { type = "button" class = "quiet small edit" value = row.id "Edit" on click(e) { emit openEdit(e.target.value) } }
  182. if (row.canDelete) { button { type = "button" class = "quiet small danger delete" value = row.id "Delete" on click(e) { emit remove(e.target.value) } } }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. TimeZone { timeZone = timeZone }
  189. }
  190. }
  191. p { id = "notice" class = "notice" notice }
  192. p { id = "message" class = "message" message }
  193. }
  194. }
  195. on setF(name, v) {
  196. if (name == 'identityName') { fIdentityName = v }
  197. if (name == 'nickname') { fNickname = v }
  198. if (name == 'firstname') { fFirstname = v }
  199. if (name == 'lastname') { fLastname = v }
  200. if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }
  201. }
  202. // 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.
  203. // THE SESSION REMEMBERS THE STEP, then the browser goes to the code page. A function route gets no session (hybriel#11), so the
  204. // face records it; it refuses unless a code for the address is really waiting.
  205. emailSent = (String address) => {
  206. k = emit server rememberPending(address)
  207. if (k == null) {
  208. message = 'the server did not answer — try again'
  209. return null
  210. }
  211. if (k.error != null) {
  212. message = k.error
  213. return null
  214. }
  215. navigate(codeUrl)
  216. }
  217. // the code step (components/codestep.hl) checked the code: why not, or the signed-in account
  218. codeFailed = (String text) => { message = text }
  219. codeVerified = (r) => {
  220. askCode = false
  221. // signed in: the address bar leaves the code page (a reload of /code would go back
  222. // to it anyway — lib/api.hl codePage sends a signed-in browser there)
  223. window.history.replaceState(null, '', homeUrl)
  224. meEmail = r.account.email
  225. timeZone = r.account.timeZone
  226. identities = r.identities
  227. signedIn = true
  228. notice = ''
  229. manage = !forApp
  230. choosing = forApp
  231. // the shell (components/main.hl) shows the user menu from the account: a page's emit reaches every level above it
  232. // (hybriel#193), so the header follows the in-place sign-in without a reload — the welcome form below stays in place
  233. emit shellSession(r.account.email)
  234. // THE FIRST LOGIN: the default identity's names, optional (for an app too: the
  235. // choice follows once the form is saved or skipped)
  236. if (r.created) {
  237. fillForm(r.identities[0])
  238. editHeading = 'Welcome — name your default identity'
  239. welcome = true
  240. notWelcome = false
  241. editing = true
  242. choosing = false
  243. }
  244. }
  245. // THE CHOICE: the server makes (or finds) this identity's id for the app and answers
  246. // the app's return URL with a one-time code — the browser goes there
  247. on choose(id) {
  248. message = ''
  249. notice = ''
  250. r = emit server chooseIdentity(rid, '' + id)
  251. if (r == null) {
  252. message = 'the server did not answer — try again'
  253. return null
  254. }
  255. if (r.error != null) {
  256. message = r.error
  257. return null
  258. }
  259. notice = 'Back to ' + appName + ' …'
  260. window.location.assign(r.url)
  261. }
  262. fillForm = (row) => {
  263. editId = row.id
  264. fIdentityName = row.identityName
  265. fNickname = row.nickname
  266. fFirstname = row.firstname
  267. fLastname = row.lastname
  268. fAvatar = row.avatar
  269. hasAvatarPreview = row.hasAvatar
  270. return null
  271. }
  272. rowOf = (id) => {
  273. for (row of identities) { if ('' + row.id == '' + id) { return row } }
  274. return null
  275. }
  276. on openNew(e) {
  277. message = ''
  278. notice = ''
  279. editId = ''
  280. fIdentityName = ''
  281. fNickname = ''
  282. fFirstname = ''
  283. fLastname = ''
  284. fAvatar = ''
  285. hasAvatarPreview = false
  286. editHeading = 'New identity'
  287. welcome = false
  288. notWelcome = true
  289. editing = true
  290. }
  291. on openEdit(id) {
  292. message = ''
  293. notice = ''
  294. row = rowOf(id)
  295. if (row == null) { return null }
  296. fillForm(row)
  297. editHeading = 'Edit ' + row.label
  298. welcome = false
  299. notWelcome = true
  300. editing = true
  301. }
  302. on closeForm(e) {
  303. editing = false
  304. welcome = false
  305. notWelcome = true
  306. message = ''
  307. choosing = forApp
  308. }
  309. on saveForm(e) {
  310. message = ''
  311. fields = { identityName = fIdentityName nickname = fNickname firstname = fFirstname lastname = fLastname avatar = fAvatar }
  312. let r = null
  313. if (editId == '' && welcome) {
  314. r = emit server editIdentity('' + welcomeId, fields)
  315. } else if (editId == '') {
  316. r = emit server addIdentity(fields)
  317. } else {
  318. r = emit server editIdentity('' + editId, fields)
  319. }
  320. if (r == null) {
  321. message = 'the server did not answer — try again'
  322. return null
  323. }
  324. if (r.error != null) {
  325. message = r.error
  326. return null
  327. }
  328. identities = r.identities
  329. notice = editId == '' && !welcome ? 'Identity created.' : 'Identity saved.'
  330. editing = false
  331. welcome = false
  332. notWelcome = true
  333. choosing = forApp
  334. }
  335. on remove(id) {
  336. message = ''
  337. notice = ''
  338. row = rowOf(id)
  339. if (row == null) { return null }
  340. if (!confirm('Delete the identity “' + row.label + '”?')) { return null }
  341. r = emit server dropIdentity('' + id)
  342. if (r == null) {
  343. message = 'the server did not answer — try again'
  344. return null
  345. }
  346. if (r.error != null) {
  347. message = r.error
  348. return null
  349. }
  350. identities = r.identities
  351. if ('' + editId == '' + id) { editing = false }
  352. notice = 'Identity deleted.'
  353. }
  354. // the time zone component (components/timezone.hl) saved the account's zone
  355. zoneSaved = (String zone) => {
  356. timeZone = zone
  357. notice = 'Time zone saved.'
  358. }
  359. // a placed component shows its answer here (the #message and #notice lines under the card)
  360. status = (String text, String note) => {
  361. message = text
  362. notice = note
  363. }
  364. // "Other address": the session forgets the pending sign-in, back to the email form
  365. back = () => {
  366. message = ''
  367. r = emit server forgetPending()
  368. navigate(homeUrl)
  369. }
  370. // ---- the faces ------------------------------------------------------------------------
  371. // A face's LAST parameter is the session (the framework appends it). Every face that
  372. // needs the account asks accountOfSession, which only accepts the framework's Session.
  373. // A face runs on a BLANK instance: it has its arguments and its imports, not this
  374. // component's members or functions.
  375. // (the code request is NOT a face since mission 010: POST /api/code in lib/api.hl — a face
  376. // sees no request headers, so it could not apply the per-IP limit; a face here would be a
  377. // way around it)
  378. on server addIdentity(fields, session) {
  379. let me = accountOfSession(&session)
  380. if (me == null) { return { error = 'you are not signed in' } }
  381. r = createIdentity(me.id, fields)
  382. if (r.error != null) { return r }
  383. return { identity = r.identity identities = identityRows(me.id) }
  384. }
  385. on server editIdentity(id, fields, session) {
  386. let me = accountOfSession(&session)
  387. if (me == null) { return { error = 'you are not signed in' } }
  388. r = updateIdentity(me.id, id, fields)
  389. if (r.error != null) { return r }
  390. return { identity = r.identity identities = identityRows(me.id) }
  391. }
  392. on server dropIdentity(id, session) {
  393. let me = accountOfSession(&session)
  394. if (me == null) { return { error = 'you are not signed in' } }
  395. r = deleteIdentity(me.id, id)
  396. if (r.error != null) { return r }
  397. return { deleted = r.deleted identities = identityRows(me.id) }
  398. }
  399. // the identity for the app's login request (lib/apps.hl grantRequest: a plain login, or an invite's): { url } or { error }
  400. on server chooseIdentity(rid, identity, session) {
  401. let me = accountOfSession(&session)
  402. if (me == null) { return { error = 'you are not signed in' } }
  403. if (rid == null || hlTypeName(rid) != 'String') { return { error = 'field rid must be a string' } }
  404. if (identity == null || hlTypeName(identity) != 'String') { return { error = 'field identity must be a string' } }
  405. return grantRequest(me.id, rid, identity)
  406. }
  407. // THE PENDING SIGN-IN (ident#20): recorded after POST /api/code answered, only while a
  408. // code for that address is really waiting (lib/login.hl recordPending); "Other address"
  409. // forgets it. Named apart from the store functions (faces dispatch by name, hybriel#36).
  410. on server rememberPending(email, session) {
  411. return recordPending(&session, email)
  412. }
  413. on server forgetPending(session) {
  414. return dropPending(&session)
  415. }

Branches

Latest commits

  • 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
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre