Sign-in & shared code

Sessions & access checks

Pathshared/sessions
Statuslive
Drill-down pages—
Pop-ups—
Tabs—
API routes—

Business rules

Data comes from: dh_users (status, role, staff link), dh_user_access (page keys), staff (staff information done or not), dh_api_keys (scripts and the extension that use an API key instead of a login).

How it works:

  • Every /api call needs a signed-in session token or a valid API key (middleware/auth.ts). Session tokens last 12 hours.
  • Who is calling is read from the database on each request, not from the token (utils/caller.ts), so a role or access change applies at once.
  • Each route checks its own page key on the server (lib/require-access.ts). Admins pass every check. The sidebar only hides menu items; it does not guard anything.
  • Staff logins may use only their own staff portal, and only the Staff information form until it is complete (middleware/staff-gate.ts).
  • In the browser, contexts/AuthContext.tsx keeps the token, and services/api.ts (authFetch) sends it. A 401 answer signs the browser out.
  • Sessions end when the password changes: a password change, a reset, an admin reset, or invite set-up (migration 175 password_changed_at). They also end when an admin deactivates the login, so reactivating it later doesn't bring old sessions back.
  • Requests already on their way get 15 seconds' grace, so other open tabs aren't signed out mid-change.
  • An inactive login is refused on every request, not only at sign-in.
  • The browser that changed its own password gets a new token and stays signed in. Other tabs in that browser pick up the new token.
  • Only a refused session signs the browser out; a server error while checking it does not.
  • A request with an ended session but a valid API key is let in by the key.

Database tables & columns

TableColumns used
dh_api_keyscreated_at, id, key_hash, last_used_at, name, permissions
dh_user_accessaccess_key, position, user_id
dh_usersaccess, email, id, name, password_changed_at, role, staff_id, status
staffid, onboarding_completed_at

File structure

Its own files

  • apps/
    • web/
      • src/
        • contexts/
          • AuthContext.tsx
        • services/
          • api.ts
    • worker/
      • src/
        • lib/
          • require-access.ts
        • middleware/
          • auth.ts
          • staff-gate.ts
        • services/
          • session.ts
          • user-access.ts
        • utils/
          • caller.ts

Shared code it uses (followed through its imports and the helpers its API calls; sign-in/access plumbing and UI primitives left out)

  • apps/
    • worker/
      • src/
        • db/
          • index.ts
        • routes/
          • auth.ts
        • services/
          • staff-onboarding.ts
        • utils/
          • jwt-key.ts