LifeMate — Personal, Family & Learning Companion
feat/standalone-weekly-planner
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | import jwt from 'jsonwebtoken'; |
| 2 | import { childSandboxEnabled, childSessionRouteAllowed, managedChildAccessActive } from './child_session_policy.js'; |
| 3 | |
| 4 | // Every API router must check both the session and the current account state. |
| 5 | export function createSessionAuth({ pool, jwtSecret, env = process.env }) { |
| 6 | return async (req, res, next) => { |
| 7 | const raw = req.headers.authorization?.replace(/^Bearer\s+/i, ''); |
| 8 | try { |
| 9 | const identity = jwt.verify(raw, jwtSecret, { |
| 10 | audience: 'lifemate-api', |
| 11 | issuer: 'lifemate', |
| 12 | }); |
| 13 | const session = await pool.query( |
| 14 | `select s.access_scope,s.child_issuer_user_id,u.is_managed_child |
| 15 | from auth_session s join app_user u on u.id=s.user_id |
| 16 | where s.id=$1 and s.user_id=$2 and s.revoked_at is null |
| 17 | and s.expires_at>now() and u.disabled_at is null`, |
| 18 | [identity.sid, identity.sub], |
| 19 | ); |
| 20 | if (!session.rowCount) return res.status(401).json({ error: 'session_invalid' }); |
| 21 | const row = session.rows[0]; |
| 22 | if (row.is_managed_child || row.access_scope === 'child_education') { |
| 23 | res.setHeader('Cache-Control', 'no-store'); |
| 24 | if (!childSandboxEnabled(env)) return res.status(503).json({ error: 'child_identity_sandbox_disabled' }); |
| 25 | if (!row.is_managed_child || row.access_scope !== 'child_education') return res.status(401).json({ error: 'session_invalid' }); |
| 26 | if (!await managedChildAccessActive(pool, identity.sub, row.child_issuer_user_id, env)) return res.status(401).json({ error: 'session_invalid' }); |
| 27 | if (!childSessionRouteAllowed(req)) return res.status(403).json({ error: 'child_session_scope_denied' }); |
| 28 | } |
| 29 | // Claims supplied in a JWT cannot override the database scope/marker. |
| 30 | identity.accessScope = row.access_scope; |
| 31 | identity.managedChild = row.is_managed_child === true; |
| 32 | req.identity = identity; |
| 33 | next(); |
| 34 | } catch { |
| 35 | res.status(401).json({ error: 'unauthorized' }); |
| 36 | } |
| 37 | }; |
| 38 | } |