ident
All repositories: gitoria
17.2 KB
// components/home.hl — `/` and `/signin/:rid`: ALL OF IDENT (pieces 1+2, tickets #24 #25;// CONCEPT.md).// Signed out: email → a six-digit code by mail → signed in. There is no registration:// the first right code for an address creates the account with a DEFAULT IDENTITY, then// this page shows that identity's name fields and says they are optional (Skip).// Signed in: the account's identities (list, new, edit, delete — never the last one)// and its time zone (the browser's at the first login; changeable here).//// Under `/signin/<rid>` the page belongs to an APP'S LOGIN BUTTON (lib/apps.hl requestOf):// it names the app, signs in to ident if needed, then asks WHICH IDENTITY the app gets// (with a single identity it still shows which one, one click) and sends the browser back// to the app with a one-time code. Managing identities stays on `/`.//// Every step takes the server's answer from the CALL (the value form of emit): it rides// the ack, over the socket or the POST fallback.parent './main.hl'import { accountOfSession } from '../lib/accounts.hl'import { recordPending, dropPending } from '../lib/login.hl'import { identityRows, createIdentity, updateIdentity, deleteIdentity } from '../lib/identities.hl'import { requestOf, grantRequest } from '../lib/apps.hl'import { navigate } from 'hl:web'import EmailStep from './emailstep.hl'import CodeStep from './codestep.hl'import TimeZone from './timezone.hl'import { siteName } from '../project.hl'session = nullrid = null// 'code' on THE CODE PAGE `/code` and `/signin/<rid>/code` (ident#20; lib/api.hl codePage// renders this component there, only while the session has a pending code), else nullstep = nullonCodePage = step == 'code'// the pending address, read by codePage from the session and handed in as a param (mission// 048): read here from the session, hl:web (64527baa) would re-derive it after EVERY face// that takes the session — after the 5th wrong code it turned null and the code form// vanished with nothing to click (gate "after 5 wrong tries even the RIGHT code is refused")pending = null// where this login lives: the email form, and the code page after "Send me a code"homeUrl = rid != null ? '/signin/' + rid : '/'codeUrl = rid != null ? '/signin/' + rid + '/code' : '/code'me = accountOfSession(&session)signedIn = me != null// THE APP'S LOGIN REQUEST (null on `/`, or when unknown / expired)request = rid != null ? requestOf(rid) : nullforApp = request != nullbadRequest = rid != null && request == nullappName = request != null ? request.app.name : ''appOrigin = request != null ? request.origin : ''forInvite = request != null && request.invite != ''requestLead = forInvite ? 'You are invited to ' : 'Sign in to 'onWelcome = step == 'welcome' && me != null && forApp // a NEW address sent here from the selector's frame (ident#31): the default identity's form firstchoosing = forApp && me != null && !onWelcome // the identity choice for the appmanage = rid == null && me != null // identities + time zone (only on `/`)meEmail = me != null ? me.email : ''timeZone = me != null ? me.timeZone : ''identities = me != null ? identityRows(me.id) : []__title = siteName + (forApp ? ' | sign in to ' + appName : (me != null ? ' | your identities' : ' | sign in'))// the login steps (a View's `if` takes a member, so each step is one).// THE CODE STEP IS ITS OWN PAGE (ident#20, creator: "just make a /code where it checks a// pending code"): "Send me a code" records the address in this browser's session (face// rememberPending) and goes to `/code`; that page shows the code form for the session's// pending address (lib/login.hl pendingOf) — so a reload, a second tab or a re-seed keeps it.pendingEmail = onCodePage && me == null && !badRequest ? pending : nullaskEmail = me == null && !badRequest && !onCodePageaskCode = pendingEmail != nullsentTo = pendingEmail != null ? pendingEmail : ''message = ''notice = ''// THE IDENTITY FORM — new, edit, and right after the first login the default identity// ("welcome": the names are optional, Skip closes it)editing = onWelcomewelcome = onWelcomenotWelcome = !onWelcome // `if (!welcome)` in a View renders nothing: an `if` takes a membereditId = '' // '' = a new identity, else the identity's id (a UUID)// 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// expression is recomputed when `identities` changes, which reset the id of every identity edited on `/`)welcomeId = onWelcome ? '' + identities[0].id : ''editHeading = onWelcome ? 'Welcome — name your default identity' : ''fIdentityName = ''fNickname = ''fFirstname = ''fLastname = ''fAvatar = ''hasAvatarPreview = falseavatarPreviewClass = hasAvatarPreview ? 'avatar' : 'avatar hidden'avatarRemoveClass = hasAvatarPreview ? 'quiet small' : 'quiet small hidden'View {identCard {if (forApp) {appRequest { id = "apprequest" requestLead strong { id = "appname" appName } " " requestOrigin { id = "apporigin" appOrigin } }}if (badRequest) {h1 { "Login request expired" }p { id = "badrequest" class = "message" "This login request is unknown or expired. Go back to the app and start the login again." }}if (askEmail) {h1 { "Sign in" }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." }EmailStep {}}if (askCode) {h1 { "Enter your code" }CodeStep { sentTo = sentTo tail = ". It is valid for 10 minutes." }}if (signedIn) {if (editing) {form { id = "identityform"on submit(e) {e.preventDefault()emit saveForm(e)}h1 { id = "formheading" editHeading }if (welcome) {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." }}label { "Identity name " fieldHint { "(shown when you pick an identity in an app; optional)" }input { id = "fidentityname" name = "identityName" maxlength = "60" autocomplete = "off" value = fIdentityName on input(e) { emit setF('identityName', e.target.value) } }}label { "Nickname " fieldHint { "(optional)" }input { id = "fnickname" name = "nickname" maxlength = "60" autocomplete = "nickname" value = fNickname on input(e) { emit setF('nickname', e.target.value) } }}label { "First name " fieldHint { "(optional)" }input { id = "ffirstname" name = "firstname" maxlength = "60" autocomplete = "given-name" value = fFirstname on input(e) { emit setF('firstname', e.target.value) } }}label { "Last name " fieldHint { "(optional)" }input { id = "flastname" name = "lastname" maxlength = "60" autocomplete = "family-name" value = fLastname on input(e) { emit setF('lastname', e.target.value) } }}avatarField {img { id = "avatarpreview" class = avatarPreviewClass src = fAvatar alt = "" width = "48" height = "48" }label { "Avatar " fieldHint { "(optional; a picture apps can show, e.g. next to your comments)" }input { id = "favatarfile" type = "file" accept = "image/png,image/jpeg,image/webp,image/gif" }}input { id = "favatar" name = "avatar" type = "hidden" value = fAvatar on input(e) { emit setF('avatar', e.target.value) } }button { id = "avatarremove" type = "button" class = avatarRemoveClass "Remove picture" on click(e) { emit setF('avatar', '') } }span { id = "avatarnote" class = "avatar-note" }}formButtons {button { id = "saveidentity" type = "submit" "Save" }if (welcome) { button { id = "skip" type = "button" class = "quiet" "Skip" on click(e) { emit closeForm(e) } } }if (notWelcome) { button { id = "cancel" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } } }}}}if (choosing) {section { id = "choosesection"h1 { "Choose an identity" }p { class = "lead" "Which identity signs in to " strong { appName } "? The app gets only the identity's short id — no email, no names." }ul { id = "chooselist"for (row of identities) {li { class = "choice"if (row.hasAvatar) { img { class = "avatar" src = row.avatar alt = "" width = "32" height = "32" } }identityMain {identityLabel { row.label }identityShortId { class = "shortid" row.shortId }if (row.isDefault) { defaultBadge { "default" } }identityDetails { row.details }}button { type = "button" class = "choose" value = row.id "Continue" on click(e) { emit choose(e.target.value) } }}}}}}if (manage) {section { id = "identitiessection"sectionHead {h1 { "Your identities" }button { id = "newidentity" type = "button" class = "small" "New identity" on click(e) { emit openNew(e) } }}ul { id = "identities"for (row of identities) {li { class = "identity"if (row.hasAvatar) { img { class = "avatar" src = row.avatar alt = "" width = "32" height = "32" } }identityMain {identityLabel { row.label }identityShortId { class = "shortid" row.shortId }if (row.isDefault) { defaultBadge { "default" } }identityDetails { row.details }}identityActions {a { class = "used" href = row.usedHref "Used in…" }button { type = "button" class = "quiet small edit" value = row.id "Edit" on click(e) { emit openEdit(e.target.value) } }if (row.canDelete) { button { type = "button" class = "quiet small danger delete" value = row.id "Delete" on click(e) { emit remove(e.target.value) } } }}}}}}TimeZone { timeZone = timeZone }}}p { id = "notice" class = "notice" notice }p { id = "message" class = "message" message }}}on setF(name, v) {if (name == 'identityName') { fIdentityName = v }if (name == 'nickname') { fNickname = v }if (name == 'firstname') { fFirstname = v }if (name == 'lastname') { fLastname = v }if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }}// 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.// THE SESSION REMEMBERS THE STEP, then the browser goes to the code page. A function route gets no session (hybriel#11), so the// face records it; it refuses unless a code for the address is really waiting.emailSent = (String address) => {k = emit server rememberPending(address)if (k == null) {message = 'the server did not answer — try again'return null}if (k.error != null) {message = k.errorreturn null}navigate(codeUrl)}// the code step (components/codestep.hl) checked the code: why not, or the signed-in accountcodeFailed = (String text) => { message = text }codeVerified = (r) => {askCode = false// signed in: the address bar leaves the code page (a reload of /code would go back// to it anyway — lib/api.hl codePage sends a signed-in browser there)window.history.replaceState(null, '', homeUrl)meEmail = r.account.emailtimeZone = r.account.timeZoneidentities = r.identitiessignedIn = truenotice = ''manage = !forAppchoosing = forApp// the shell (components/main.hl) shows the user menu from the account: a page's emit reaches every level above it// (hybriel#193), so the header follows the in-place sign-in without a reload — the welcome form below stays in placeemit shellSession(r.account.email)// THE FIRST LOGIN: the default identity's names, optional (for an app too: the// choice follows once the form is saved or skipped)if (r.created) {fillForm(r.identities[0])editHeading = 'Welcome — name your default identity'welcome = truenotWelcome = falseediting = truechoosing = false}}// THE CHOICE: the server makes (or finds) this identity's id for the app and answers// the app's return URL with a one-time code — the browser goes thereon choose(id) {message = ''notice = ''r = emit server chooseIdentity(rid, '' + id)if (r == null) {message = 'the server did not answer — try again'return null}if (r.error != null) {message = r.errorreturn null}notice = 'Back to ' + appName + ' …'window.location.assign(r.url)}fillForm = (row) => {editId = row.idfIdentityName = row.identityNamefNickname = row.nicknamefFirstname = row.firstnamefLastname = row.lastnamefAvatar = row.avatarhasAvatarPreview = row.hasAvatarreturn null}rowOf = (id) => {for (row of identities) { if ('' + row.id == '' + id) { return row } }return null}on openNew(e) {message = ''notice = ''editId = ''fIdentityName = ''fNickname = ''fFirstname = ''fLastname = ''fAvatar = ''hasAvatarPreview = falseeditHeading = 'New identity'welcome = falsenotWelcome = trueediting = true}on openEdit(id) {message = ''notice = ''row = rowOf(id)if (row == null) { return null }fillForm(row)editHeading = 'Edit ' + row.labelwelcome = falsenotWelcome = trueediting = true}on closeForm(e) {editing = falsewelcome = falsenotWelcome = truemessage = ''choosing = forApp}on saveForm(e) {message = ''fields = { identityName = fIdentityName nickname = fNickname firstname = fFirstname lastname = fLastname avatar = fAvatar }let r = nullif (editId == '' && welcome) {r = emit server editIdentity('' + welcomeId, fields)} else if (editId == '') {r = emit server addIdentity(fields)} else {r = emit server editIdentity('' + editId, fields)}if (r == null) {message = 'the server did not answer — try again'return null}if (r.error != null) {message = r.errorreturn null}identities = r.identitiesnotice = editId == '' && !welcome ? 'Identity created.' : 'Identity saved.'editing = falsewelcome = falsenotWelcome = truechoosing = forApp}on remove(id) {message = ''notice = ''row = rowOf(id)if (row == null) { return null }if (!confirm('Delete the identity “' + row.label + '”?')) { return null }r = emit server dropIdentity('' + id)if (r == null) {message = 'the server did not answer — try again'return null}if (r.error != null) {message = r.errorreturn null}identities = r.identitiesif ('' + editId == '' + id) { editing = false }notice = 'Identity deleted.'}// the time zone component (components/timezone.hl) saved the account's zonezoneSaved = (String zone) => {timeZone = zonenotice = 'Time zone saved.'}// a placed component shows its answer here (the #message and #notice lines under the card)status = (String text, String note) => {message = textnotice = note}// "Other address": the session forgets the pending sign-in, back to the email formback = () => {message = ''r = emit server forgetPending()navigate(homeUrl)}// ---- the faces ------------------------------------------------------------------------// A face's LAST parameter is the session (the framework appends it). Every face that// needs the account asks accountOfSession, which only accepts the framework's Session.// A face runs on a BLANK instance: it has its arguments and its imports, not this// component's members or functions.// (the code request is NOT a face since mission 010: POST /api/code in lib/api.hl — a face// sees no request headers, so it could not apply the per-IP limit; a face here would be a// way around it)on server addIdentity(fields, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = createIdentity(me.id, fields)if (r.error != null) { return r }return { identity = r.identity identities = identityRows(me.id) }}on server editIdentity(id, fields, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = updateIdentity(me.id, id, fields)if (r.error != null) { return r }return { identity = r.identity identities = identityRows(me.id) }}on server dropIdentity(id, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = deleteIdentity(me.id, id)if (r.error != null) { return r }return { deleted = r.deleted identities = identityRows(me.id) }}// the identity for the app's login request (lib/apps.hl grantRequest: a plain login, or an invite's): { url } or { error }on server chooseIdentity(rid, identity, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }if (rid == null || hlTypeName(rid) != 'String') { return { error = 'field rid must be a string' } }if (identity == null || hlTypeName(identity) != 'String') { return { error = 'field identity must be a string' } }return grantRequest(me.id, rid, identity)}// THE PENDING SIGN-IN (ident#20): recorded after POST /api/code answered, only while a// code for that address is really waiting (lib/login.hl recordPending); "Other address"// forgets it. Named apart from the store functions (faces dispatch by name, hybriel#36).on server rememberPending(email, session) {return recordPending(&session, email)}on server forgetPending(session) {return dropPending(&session)}
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