ident
All repositories: gitoria
10.7 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 } from '../lib/identities.hl'import { requestOf } from '../lib/apps.hl'import { navigate } from 'hl:web'import EmailStep from './emailstep.hl'import CodeStep from './codestep.hl'import IdentityForm from './identityform.hl'import ChooseIdentity from './chooseidentity.hl'import IdentityList from './identitylist.hl'import TimeZone from './timezone.hl'import { siteName } from '../project.hl'import { leadLook, messageLook, noticeLook } from './styles.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 = onWelcomeeditId = '' // '' = 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 = falseStyle {pLead = leadLookpMessage = messageLookpNotice = noticeLookappRequest {display = 'block'padding = '.7rem .8rem'color = 'var(--color-text)'background = 'var(--color-surface)'borderRadius = 'var(--radius-large)'overflowWrap = 'anywhere'}requestOrigin {display = 'block'color = 'var(--color-text-muted)'fontSize = '.875rem'}}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" Style.pMessage "This login request is unknown or expired. Go back to the app and start the login again." }}if (askEmail) {h1 { "Sign in" }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." }EmailStep {}}if (askCode) {h1 { "Enter your code" }CodeStep { sentTo = sentTo tail = ". It is valid for 10 minutes." }}if (signedIn) {if (editing) {IdentityForm { editHeading = editHeading welcome = welcome editId = editId welcomeId = welcomeId fIdentityName = fIdentityName fNickname = fNickname fFirstname = fFirstname fLastname = fLastname fAvatar = fAvatar hasAvatarPreview = hasAvatarPreview }}if (choosing) {ChooseIdentity { rid = rid appName = appName identities = identities }}if (manage) {IdentityList { identities = identities }TimeZone { timeZone = timeZone }}}p { id = "notice" Style.pNotice notice }p { id = "message" Style.pMessage message }}}// a placed component shows its answer here (the #message and #notice lines under the card)status = (String text, String note) => {message = textnotice = note}// 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 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 = trueediting = truechoosing = false}}// THE IDENTITY FORM (components/identityform.hl) is opened with the values to start from; typing stays in the child.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}openNew = () => {message = ''notice = ''editId = ''fIdentityName = ''fNickname = ''fFirstname = ''fLastname = ''fAvatar = ''hasAvatarPreview = falseeditHeading = 'New identity'welcome = falseediting = true}openEdit = (id) => {message = ''notice = ''row = rowOf(id)if (row == null) { return null }fillForm(row)editHeading = 'Edit ' + row.labelwelcome = falseediting = true}closeForm = () => {editing = falsewelcome = falsemessage = ''choosing = forApp}formSaved = (rows, String text) => {identities = rowsnotice = textediting = falsewelcome = falsechoosing = forApp}// the form saved an EDIT: only that row changes (same place in the list, same position-dependent parts), so it is upserted by its idrowSaved = (row, String text) => {identities.upsert(row)notice = textediting = falsewelcome = falsechoosing = forApp}// the identity list (components/identitylist.hl) deleted an identityidentitiesDeleted = (rows, String id) => {identities = rowsif ('' + 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.'}// "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)// 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
- 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
- 5d1b3fedMerge branch 'v61-day61' into mainIdent
- 83faeb15|||PR ident: vendor Hybriel day61mre
- b2572b34Merge branch 'v59-day59' into mainIdent
- 67cf016d|||PR ident: vendor Hybriel day59mre
- 600c043dMerge branch 'v58-day58' into mainIdent
- 365f8c80|||PR ident: vendor Hybriel day58mre
- 585126e5Merge branch 'f16-setf' into mainIdent
- 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
- a1419d9cMerge branch 'f15-navigate' into mainIdent
- 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
- e36f4516Merge branch 'f14-online' into mainIdent
- 2c5eac91|||PR ident#38 online state without probe timermre
- 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