gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8cbc57178cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre8cbc5717/components/home.hl

18.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, setTimeZone } 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 { siteName } from '../project.hl'
  25. session = null
  26. rid = null
  27. // 'code' on THE CODE PAGE `/code` and `/signin/<rid>/code` (ident#20; lib/api.hl codePage
  28. // renders this component there, only while the session has a pending code), else null
  29. step = null
  30. onCodePage = step == 'code'
  31. // the pending address, read by codePage from the session and handed in as a param (mission
  32. // 048): read here from the session, hl:web (64527baa) would re-derive it after EVERY face
  33. // that takes the session — after the 5th wrong code it turned null and the code form
  34. // vanished with nothing to click (gate "after 5 wrong tries even the RIGHT code is refused")
  35. pending = null
  36. // where this login lives: the email form, and the code page after "Send me a code"
  37. homeUrl = rid != null ? '/signin/' + rid : '/'
  38. codeUrl = rid != null ? '/signin/' + rid + '/code' : '/code'
  39. me = accountOfSession(&session)
  40. signedIn = me != null
  41. // THE APP'S LOGIN REQUEST (null on `/`, or when unknown / expired)
  42. request = rid != null ? requestOf(rid) : null
  43. forApp = request != null
  44. badRequest = rid != null && request == null
  45. appName = request != null ? request.app.name : ''
  46. appOrigin = request != null ? request.origin : ''
  47. forInvite = request != null && request.invite != ''
  48. requestLead = forInvite ? 'You are invited to ' : 'Sign in to '
  49. onWelcome = step == 'welcome' && me != null && forApp // a NEW address sent here from the selector's frame (ident#31): the default identity's form first
  50. choosing = forApp && me != null && !onWelcome // the identity choice for the app
  51. manage = rid == null && me != null // identities + time zone (only on `/`)
  52. meEmail = me != null ? me.email : ''
  53. timeZone = me != null ? me.timeZone : ''
  54. tzInput = 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. section { id = "timezonesection"
  189. h2 { "Time zone" }
  190. p { class = "lead" "Taken from your browser at your first login. Currently " strong { id = "timezone" timeZone } "." }
  191. p { id = "digestnote" class = "lead" "Your daily mail of notifications comes at 17:00 in this time zone." }
  192. form { id = "tzform"
  193. on submit(e) {
  194. e.preventDefault()
  195. emit saveZone(e)
  196. }
  197. label { "Time zone (IANA name, e.g. Europe/Vienna)"
  198. input { id = "tzinput" name = "timeZone" autocomplete = "off" maxlength = "64" required = "required" value = tzInput on input(e) { emit setTz(e.target.value) } }
  199. }
  200. formButtons {
  201. button { id = "savetz" type = "submit" "Save time zone" }
  202. button { id = "browsertz" type = "button" class = "quiet" "Use this browser's" on click(e) { emit useBrowserZone(e) } }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. p { id = "notice" class = "notice" notice }
  209. p { id = "message" class = "message" message }
  210. }
  211. }
  212. on setTz(v) { tzInput = v }
  213. on setF(name, v) {
  214. if (name == 'identityName') { fIdentityName = v }
  215. if (name == 'nickname') { fNickname = v }
  216. if (name == 'firstname') { fFirstname = v }
  217. if (name == 'lastname') { fLastname = v }
  218. if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }
  219. }
  220. // the browser's own time zone (IANA name) and whether a name is one it knows (`Intl` is a
  221. // browser global of client code, hybriel#18)
  222. browserZone = () => { return Intl.DateTimeFormat().resolvedOptions().timeZone }
  223. knownZone = (v) => { return v == 'UTC' || Intl.supportedValuesOf('timeZone').includes(v) }
  224. // 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.
  225. // THE SESSION REMEMBERS THE STEP, then the browser goes to the code page. A function route gets no session (hybriel#11), so the
  226. // face records it; it refuses unless a code for the address is really waiting.
  227. emailSent = (String address) => {
  228. k = emit server rememberPending(address)
  229. if (k == null) {
  230. message = 'the server did not answer — try again'
  231. return null
  232. }
  233. if (k.error != null) {
  234. message = k.error
  235. return null
  236. }
  237. navigate(codeUrl)
  238. }
  239. // the code step (components/codestep.hl) checked the code: why not, or the signed-in account
  240. codeFailed = (String text) => { message = text }
  241. codeVerified = (r) => {
  242. askCode = false
  243. // signed in: the address bar leaves the code page (a reload of /code would go back
  244. // to it anyway — lib/api.hl codePage sends a signed-in browser there)
  245. window.history.replaceState(null, '', homeUrl)
  246. meEmail = r.account.email
  247. timeZone = r.account.timeZone
  248. tzInput = r.account.timeZone
  249. identities = r.identities
  250. signedIn = true
  251. notice = ''
  252. manage = !forApp
  253. choosing = forApp
  254. // the shell (components/main.hl) shows the user menu from the account: a page's emit reaches every level above it
  255. // (hybriel#193), so the header follows the in-place sign-in without a reload — the welcome form below stays in place
  256. emit shellSession(r.account.email)
  257. // THE FIRST LOGIN: the default identity's names, optional (for an app too: the
  258. // choice follows once the form is saved or skipped)
  259. if (r.created) {
  260. fillForm(r.identities[0])
  261. editHeading = 'Welcome — name your default identity'
  262. welcome = true
  263. notWelcome = false
  264. editing = true
  265. choosing = false
  266. }
  267. }
  268. // THE CHOICE: the server makes (or finds) this identity's id for the app and answers
  269. // the app's return URL with a one-time code — the browser goes there
  270. on choose(id) {
  271. message = ''
  272. notice = ''
  273. r = emit server chooseIdentity(rid, '' + id)
  274. if (r == null) {
  275. message = 'the server did not answer — try again'
  276. return null
  277. }
  278. if (r.error != null) {
  279. message = r.error
  280. return null
  281. }
  282. notice = 'Back to ' + appName + ' …'
  283. window.location.assign(r.url)
  284. }
  285. fillForm = (row) => {
  286. editId = row.id
  287. fIdentityName = row.identityName
  288. fNickname = row.nickname
  289. fFirstname = row.firstname
  290. fLastname = row.lastname
  291. fAvatar = row.avatar
  292. hasAvatarPreview = row.hasAvatar
  293. return null
  294. }
  295. rowOf = (id) => {
  296. for (row of identities) { if ('' + row.id == '' + id) { return row } }
  297. return null
  298. }
  299. on openNew(e) {
  300. message = ''
  301. notice = ''
  302. editId = ''
  303. fIdentityName = ''
  304. fNickname = ''
  305. fFirstname = ''
  306. fLastname = ''
  307. fAvatar = ''
  308. hasAvatarPreview = false
  309. editHeading = 'New identity'
  310. welcome = false
  311. notWelcome = true
  312. editing = true
  313. }
  314. on openEdit(id) {
  315. message = ''
  316. notice = ''
  317. row = rowOf(id)
  318. if (row == null) { return null }
  319. fillForm(row)
  320. editHeading = 'Edit ' + row.label
  321. welcome = false
  322. notWelcome = true
  323. editing = true
  324. }
  325. on closeForm(e) {
  326. editing = false
  327. welcome = false
  328. notWelcome = true
  329. message = ''
  330. choosing = forApp
  331. }
  332. on saveForm(e) {
  333. message = ''
  334. fields = { identityName = fIdentityName nickname = fNickname firstname = fFirstname lastname = fLastname avatar = fAvatar }
  335. let r = null
  336. if (editId == '' && welcome) {
  337. r = emit server editIdentity('' + welcomeId, fields)
  338. } else if (editId == '') {
  339. r = emit server addIdentity(fields)
  340. } else {
  341. r = emit server editIdentity('' + editId, fields)
  342. }
  343. if (r == null) {
  344. message = 'the server did not answer — try again'
  345. return null
  346. }
  347. if (r.error != null) {
  348. message = r.error
  349. return null
  350. }
  351. identities = r.identities
  352. notice = editId == '' && !welcome ? 'Identity created.' : 'Identity saved.'
  353. editing = false
  354. welcome = false
  355. notWelcome = true
  356. choosing = forApp
  357. }
  358. on remove(id) {
  359. message = ''
  360. notice = ''
  361. row = rowOf(id)
  362. if (row == null) { return null }
  363. if (!confirm('Delete the identity “' + row.label + '”?')) { return null }
  364. r = emit server dropIdentity('' + id)
  365. if (r == null) {
  366. message = 'the server did not answer — try again'
  367. return null
  368. }
  369. if (r.error != null) {
  370. message = r.error
  371. return null
  372. }
  373. identities = r.identities
  374. if ('' + editId == '' + id) { editing = false }
  375. notice = 'Identity deleted.'
  376. }
  377. on useBrowserZone(e) { tzInput = browserZone() }
  378. on saveZone(e) {
  379. message = ''
  380. notice = ''
  381. v = tzInput.trim()
  382. if (!knownZone(v)) {
  383. message = 'this browser does not know the time zone “' + v + '”'
  384. return null
  385. }
  386. r = emit server changeTimeZone(v)
  387. if (r == null) {
  388. message = 'the server did not answer — try again'
  389. return null
  390. }
  391. if (r.error != null) {
  392. message = r.error
  393. return null
  394. }
  395. timeZone = r.timeZone
  396. tzInput = r.timeZone
  397. notice = 'Time zone saved.'
  398. }
  399. // "Other address": the session forgets the pending sign-in, back to the email form
  400. back = () => {
  401. message = ''
  402. r = emit server forgetPending()
  403. navigate(homeUrl)
  404. }
  405. // ---- the faces ------------------------------------------------------------------------
  406. // A face's LAST parameter is the session (the framework appends it). Every face that
  407. // needs the account asks accountOfSession, which only accepts the framework's Session.
  408. // A face runs on a BLANK instance: it has its arguments and its imports, not this
  409. // component's members or functions.
  410. // (the code request is NOT a face since mission 010: POST /api/code in lib/api.hl — a face
  411. // sees no request headers, so it could not apply the per-IP limit; a face here would be a
  412. // way around it)
  413. on server addIdentity(fields, session) {
  414. let me = accountOfSession(&session)
  415. if (me == null) { return { error = 'you are not signed in' } }
  416. r = createIdentity(me.id, fields)
  417. if (r.error != null) { return r }
  418. return { identity = r.identity identities = identityRows(me.id) }
  419. }
  420. on server editIdentity(id, fields, session) {
  421. let me = accountOfSession(&session)
  422. if (me == null) { return { error = 'you are not signed in' } }
  423. r = updateIdentity(me.id, id, fields)
  424. if (r.error != null) { return r }
  425. return { identity = r.identity identities = identityRows(me.id) }
  426. }
  427. on server dropIdentity(id, session) {
  428. let me = accountOfSession(&session)
  429. if (me == null) { return { error = 'you are not signed in' } }
  430. r = deleteIdentity(me.id, id)
  431. if (r.error != null) { return r }
  432. return { deleted = r.deleted identities = identityRows(me.id) }
  433. }
  434. on server changeTimeZone(tz, session) {
  435. let me = accountOfSession(&session)
  436. if (me == null) { return { error = 'you are not signed in' } }
  437. r = setTimeZone(me.id, tz)
  438. if (r.error != null) { return r }
  439. return { timeZone = r.account.timeZone }
  440. }
  441. // the identity for the app's login request (lib/apps.hl grantRequest: a plain login, or an invite's): { url } or { error }
  442. on server chooseIdentity(rid, identity, session) {
  443. let me = accountOfSession(&session)
  444. if (me == null) { return { error = 'you are not signed in' } }
  445. if (rid == null || hlTypeName(rid) != 'String') { return { error = 'field rid must be a string' } }
  446. if (identity == null || hlTypeName(identity) != 'String') { return { error = 'field identity must be a string' } }
  447. return grantRequest(me.id, rid, identity)
  448. }
  449. // THE PENDING SIGN-IN (ident#20): recorded after POST /api/code answered, only while a
  450. // code for that address is really waiting (lib/login.hl recordPending); "Other address"
  451. // forgets it. Named apart from the store functions (faces dispatch by name, hybriel#36).
  452. on server rememberPending(email, session) {
  453. return recordPending(&session, email)
  454. }
  455. on server forgetPending(session) {
  456. return dropPending(&session)
  457. }

Branches

Latest commits

  • 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
  • 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre