gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite23efdfce23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmree23efdfc/components/home.hl

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

Branches

Latest commits

  • 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
  • d277633cident#27: README: identity access and the introspect endpointmre