All files / src/app/routes ProtectedLayout.tsx

100% Statements 11/11
100% Branches 10/10
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                            10x   10x 5x             5x 2x 1x     1x                   3x                     3x 1x             2x      
import { Navigate, Outlet } from 'react-router-dom'
 
import { useSyncAuthIdentity } from '@/features/auth/api/auth.hooks'
import { NameGate } from '@/features/auth/components/NameGate'
import { LegalAcceptanceGate } from '@/features/legal/components/LegalAcceptanceGate'
import { ApiError } from '@/lib/http-client'
 
/**
 * GET /auth/me doubles as the session probe: 401 (after the http client's
 * own silent-refresh attempt already failed) means "not logged in" and
 * redirects to /login. Also hydrates authIdentity.store with the confirmed
 * profile — see useSyncAuthIdentity.
 */
export function ProtectedLayout() {
  const currentUser = useSyncAuthIdentity()
 
  if (currentUser.isPending) {
    return (
      <div className="flex min-h-screen items-center justify-center">
        <div className="border-t-transparent h-8 w-8 animate-spin rounded-full border-2 border-primary" />
      </div>
    )
  }
 
  if (currentUser.isError) {
    if (currentUser.error instanceof ApiError && currentUser.error.status === 401) {
      return <Navigate to="/login" replace />
    }
 
    return (
      <div className="flex min-h-screen items-center justify-center p-8 text-center text-ink-muted">
        {currentUser.error.message}
      </div>
    )
  }
 
  const content = (
    // The name gate is inert for every account that has one, which today is
    // all of them — see NameGate.
    <NameGate>
      <Outlet />
    </NameGate>
  )
 
  // A demo account is not asked to accept the privacy policy and terms (decided
  // 2026-10-12). Nobody signed up: the account is a sample family, made by one
  // click and deleted in 24 hours, and a consent screen between the visitor and
  // the product is the one thing the demo exists to remove. No acceptance is
  // recorded on its behalf either — a consent record for a text nobody read
  // would be a false record. The API says which accounts these are.
  if (currentUser.data.demoExpiresAt) {
    return content
  }
 
  // Inside the session gate and outside everything else: consent is asked of a
  // known person, and it has to be asked before any screen that shows a child's
  // data. Consent outermost: whether we may process this person's data is
  // settled before we ask them anything else.
  return <LegalAcceptanceGate>{content}</LegalAcceptanceGate>
 
}