gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitbf1980f2bf1980f2Merge branch 'v56-day56' into mainIdentbf1980f2/components/home.hl

11.3 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 shell (components/main.hl) shows the user menu from the account: a page's emit reaches every level above it
  174. // (hybriel#193), so the header follows the in-place sign-in without a reload — the welcome form below stays in place
  175. emit shellSession(r.account.email)
  176. // THE FIRST LOGIN: the default identity's names, optional (for an app too: the
  177. // choice follows once the form is saved or skipped)
  178. if (r.created) {
  179. fillForm(r.identities[0])
  180. editHeading = 'Welcome — name your default identity'
  181. welcome = true
  182. editing = true
  183. choosing = false
  184. }
  185. }
  186. // THE IDENTITY FORM (components/identityform.hl) is opened with the values to start from; what the user types is handed up by setF
  187. fillForm = (row) => {
  188. editId = row.id
  189. fIdentityName = row.identityName
  190. fNickname = row.nickname
  191. fFirstname = row.firstname
  192. fLastname = row.lastname
  193. fAvatar = row.avatar
  194. hasAvatarPreview = row.hasAvatar
  195. return null
  196. }
  197. rowOf = (id) => {
  198. for (row of identities) { if ('' + row.id == '' + id) { return row } }
  199. return null
  200. }
  201. openNew = () => {
  202. message = ''
  203. notice = ''
  204. editId = ''
  205. fIdentityName = ''
  206. fNickname = ''
  207. fFirstname = ''
  208. fLastname = ''
  209. fAvatar = ''
  210. hasAvatarPreview = false
  211. editHeading = 'New identity'
  212. welcome = false
  213. editing = true
  214. }
  215. openEdit = (id) => {
  216. message = ''
  217. notice = ''
  218. row = rowOf(id)
  219. if (row == null) { return null }
  220. fillForm(row)
  221. editHeading = 'Edit ' + row.label
  222. welcome = false
  223. editing = true
  224. }
  225. setF = (String name, String v) => {
  226. if (name == 'identityName') { fIdentityName = v }
  227. if (name == 'nickname') { fNickname = v }
  228. if (name == 'firstname') { fFirstname = v }
  229. if (name == 'lastname') { fLastname = v }
  230. if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }
  231. }
  232. closeForm = () => {
  233. editing = false
  234. welcome = false
  235. message = ''
  236. choosing = forApp
  237. }
  238. formSaved = (rows, String text) => {
  239. identities = rows
  240. notice = text
  241. editing = false
  242. welcome = false
  243. choosing = forApp
  244. }
  245. // 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
  246. rowSaved = (row, String text) => {
  247. identities.upsert(row)
  248. notice = text
  249. editing = false
  250. welcome = false
  251. choosing = forApp
  252. }
  253. // the identity list (components/identitylist.hl) deleted an identity
  254. identitiesDeleted = (rows, String id) => {
  255. identities = rows
  256. if ('' + editId == id) { editing = false }
  257. notice = 'Identity deleted.'
  258. }
  259. // the time zone component (components/timezone.hl) saved the account's zone
  260. zoneSaved = (String zone) => {
  261. timeZone = zone
  262. notice = 'Time zone saved.'
  263. }
  264. // "Other address": the session forgets the pending sign-in, back to the email form
  265. back = () => {
  266. message = ''
  267. r = emit server forgetPending()
  268. navigate(homeUrl)
  269. }
  270. // ---- the faces ------------------------------------------------------------------------
  271. // A face's LAST parameter is the session (the framework appends it). Every face that
  272. // needs the account asks accountOfSession, which only accepts the framework's Session.
  273. // A face runs on a BLANK instance: it has its arguments and its imports, not this
  274. // component's members or functions.
  275. // (the code request is NOT a face since mission 010: POST /api/code in lib/api.hl — a face
  276. // sees no request headers, so it could not apply the per-IP limit; a face here would be a
  277. // way around it)
  278. // THE PENDING SIGN-IN (ident#20): recorded after POST /api/code answered, only while a
  279. // code for that address is really waiting (lib/login.hl recordPending); "Other address"
  280. // forgets it. Named apart from the store functions (faces dispatch by name, hybriel#36).
  281. on server rememberPending(email, session) {
  282. return recordPending(&session, email)
  283. }
  284. on server forgetPending(session) {
  285. return dropPending(&session)
  286. }

Branches

Latest commits

  • 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
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre