Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

20.4 KB
Raw Blame
import { useState, useEffect, useMemo, useRef } from 'react'
import { useNavigate, useLocation } from 'react-router'
import { useAuthStore } from '../../store/authStore'
import { useSettingsStore, hasStoredLanguage } from '../../store/settingsStore'
import { useTranslation, detectBrowserLanguage } from '../../i18n'
import { startAuthentication } from '@simplewebauthn/browser'
import { clearSignedOut, wasSignedOut } from '../../utils/signedOut'
import { authApi, configApi } from '../../api/client'
import { getApiErrorMessage } from '../../types'
import { START_DESTINATION_ROUTE } from '../../utils/startDestination'
import { passwordErrorKey } from '../../utils/passwordError'
import { handOverToApp } from '../../native/appBoot'
import { toastHaptic } from '../../native/haptics'
import { isNativeApp } from '../../native/platform'
import {
  BrowserSignInCancelled,
  isHandoffReturn,
  rememberHandoffReturn,
  resumeBrowserSignIn,
  signInThroughBrowser,
  takeHandoffReturn,
  type BrowserSignInStart,
} from '../../native/browserSignIn'

interface AppConfig {
  has_users: boolean
  allow_registration: boolean
  setup_complete: boolean
  managed?: boolean
  demo_mode: boolean
  oidc_configured: boolean
  oidc_display_name?: string
  oidc_only_mode: boolean
  password_login: boolean
  password_registration: boolean
  oidc_login: boolean
  oidc_registration: boolean
  /** False when the admin wants the login page shown before the IdP; absent on servers from before #2925. */
  oidc_auto_redirect?: boolean
  passkey_login?: boolean
  passkey_configured?: boolean
  env_override_oidc_only: boolean
}

const CONFIG_CACHE_KEY = 'trek_app_config_cache'

/** Where an OIDC-only instance sends its users; the IdP owns the session policy (#1927). */
export const IDP_LOGIN_URL = '/api/auth/oidc/login?remember=1'

/** How long the redirect screen waits before it offers the way on by hand. */
const IDP_SLOW_MS = 8000

function readCachedConfig(): AppConfig | null {
  try {
    const raw = localStorage.getItem(CONFIG_CACHE_KEY)
    return raw ? (JSON.parse(raw) as AppConfig) : null
  } catch { return null }
}

const isOidcOnly = (config: AppConfig | null): boolean =>
  !!config && !config.password_login && config.oidc_login && config.oidc_configured

/**
 * Whether the login page leaves for the IdP by itself: an OIDC-only instance
 * does, unless its admin turned that off to show the page and its SSO button
 * first (#2925). A config without the field keeps the redirect.
 */
const redirectsToIdp = (config: AppConfig | null): boolean =>
  isOidcOnly(config) && config?.oidc_auto_redirect !== false

/**
 * Login data hook — owns the whole auth surface: login/register/demo, the MFA
 * step-up, the must-change-password step, the OIDC code exchange + error
 * handling, the app-config probe (with cache fallback) and the language
 * detection chain. LoginPage is a pure wiring container that renders what this
 * returns. Behaviour is identical to the previous in-component logic.
 */
export function useLogin() {
  const { t } = useTranslation()
  const [mode, setMode] = useState<'login' | 'register'>('login')
  const [username, setUsername] = useState<string>('')
  const [email, setEmail] = useState<string>('')
  const [password, setPassword] = useState<string>('')
  // On by default in the app: a session cookie there dies with the app process,
  // and nobody expects to sign in again every time the phone unloads an app.
  const [rememberMe, setRememberMe] = useState<boolean>(isNativeApp)
  const [nativeApp] = useState(isNativeApp)
  const [showPassword, setShowPassword] = useState<boolean>(false)
  const [isLoading, setIsLoading] = useState<boolean>(false)
  const [error, setError] = useState<string>('')
  // Set when the server signals it just issued a Secure cookie over plain HTTP —
  // the browser drops it, so we explain the fix instead of a bare 401 later.
  const [insecureCookie, setInsecureCookie] = useState(false)
  const [appConfig, setAppConfig] = useState<AppConfig | null>(null)
  // Until the config answers, the page cannot know which sign-in it offers, and
  // on a slow line the password form it drew meanwhile was the first thing an
  // OIDC-only user saw (#1167). What the last visit cached says it early: an
  // instance that sends everyone to the IdP is announced as that redirect.
  const [configLoaded, setConfigLoaded] = useState(false)
  const [cachedConfig] = useState(readCachedConfig)
  const [expectRedirect] = useState(() => {
    const params = new URLSearchParams(window.location.search)
    return !isNativeApp() && redirectsToIdp(cachedConfig) && !!cachedConfig?.has_users
      && !params.get('invite') && !params.get('oidc_code') && !params.get('oidc_error')
  })
  const [idpRedirect, setIdpRedirect] = useState(false)
  const [idpSlow, setIdpSlow] = useState(false)
  const [inviteToken, setInviteToken] = useState<string>('')
  const [inviteValid, setInviteValid] = useState<boolean>(false)
  const exchangeInitiated = useRef(false)

  const [langDropdownOpen, setLangDropdownOpen] = useState<boolean>(false)

  const [showTakeoff, setShowTakeoff] = useState<boolean>(false)
  const [mfaStep, setMfaStep] = useState(false)
  const [mfaToken, setMfaToken] = useState('')
  const [mfaCode, setMfaCode] = useState('')
  const [passwordChangeStep, setPasswordChangeStep] = useState(false)
  const [savedLoginPassword, setSavedLoginPassword] = useState('')
  const [newPassword, setNewPassword] = useState('')
  const [confirmPassword, setConfirmPassword] = useState('')

  const { login, register, demoLogin, completeMfaLogin, loadUser } = useAuthStore()
  const { setLanguageLocal, setLanguageTransient, loadSettings } = useSettingsStore()
  const navigate = useNavigate()
  const location = useLocation()
  // Location state alone is not enough: a deliberate sign-out loses it to
  // ProtectedRoute's stateless <Navigate replace>, and to any full document
  // load. The per-tab marker survives both — see utils/signedOut (#2123).
  const noRedirect = !!(location.state as { noRedirect?: boolean } | null)?.noRedirect || wasSignedOut()
  const redirectScreen = idpRedirect || (!configLoaded && expectRedirect && !noRedirect)
  const configWait = !configLoaded && !appConfig && !redirectScreen
  const idpName = (appConfig ?? cachedConfig)?.oidc_display_name || 'SSO'

  // A redirect that has not left the page after a while offers the way on by
  // hand, so a stalled IdP never strands anybody on a screen without a control.
  useEffect(() => {
    if (!redirectScreen) return
    const timer = window.setTimeout(() => setIdpSlow(true), IDP_SLOW_MS)
    return () => window.clearTimeout(timer)
  }, [redirectScreen])

  const redirectTarget = useMemo(() => {
    const params = new URLSearchParams(window.location.search)
    const redirect = params.get('redirect')
    // Only allow relative paths starting with / to prevent open redirect attacks
    if (redirect && redirect.startsWith('/') && !redirect.startsWith('//') && !redirect.startsWith('/\\')) {
      return redirect
    }
    // No page to return to: hand the choice to RootRedirect, which is the one
    // place that knows whether this user starts on the dashboard or in a trip.
    return START_DESTINATION_ROUTE
  }, [])

  useEffect(() => {
    if (redirectTarget !== START_DESTINATION_ROUTE) {
      sessionStorage.setItem('oidc_redirect', redirectTarget)
    }
  }, [redirectTarget])

  /**
   * Start the takeoff animation, and spend those 2.6 seconds fetching the
   * settings instead of letting them sit idle. Whoever we hand over to then
   * already has them — which matters for the startup destination, decided the
   * moment we navigate, and otherwise racing this exact request.
   */
  const takeOff = (): void => {
    setShowTakeoff(true)
    if (nativeApp) {
      // The app has no takeoff: the sign-in screen steps back and the app rises in
      // as soon as the settings are there (native/appBoot.ts).
      toastHaptic('success')
      void handOverToApp(loadSettings(), () => {
        void navigate(redirectTarget)
      })
      return
    }
    loadSettings()
    setTimeout(() => navigate(redirectTarget), 2600)
  }

  useEffect(() => {
    // Hoisted out of the `oidcCode` branch below (#2126). An exchange in flight
    // owns this page, but it strips `oidc_code` from the URL before it navigates
    // away — so while it waits on /api/auth/me and IndexedDB, a re-run of this
    // effect read an empty search, fell past the branch that used to hold the
    // guard, and reached the auto-redirect at the bottom. That bounced to the
    // IdP, which answered silently, which landed back here: the loop the
    // reporter saw flashing. Guarding on the ref alone closes it whatever
    // re-triggers the effect.
    if (exchangeInitiated.current) return

    const params = new URLSearchParams(window.location.search)

    const invite = params.get('invite')
    const oidcCode = params.get('oidc_code')
    const oidcError = params.get('oidc_error')

    // Also after a failed sign-in at the IdP, only then without the redirect: an
    // OIDC-only instance has to show its own screen and the error, not a password
    // form nobody can use.
    const loadConfig = (allowRedirect: boolean): void => {
      const request = authApi.getAppConfig?.()
      if (!request) { setConfigLoaded(true); return }
      request
        .then((config: AppConfig) => {
          try { localStorage.setItem(CONFIG_CACHE_KEY, JSON.stringify(config)) } catch { /* ignore quota errors */ }
          return { config, fromCache: false }
        })
        .catch(() => {
          const cached = readCachedConfig()
          return { config: cached, fromCache: !!cached }
        })
        .then(({ config, fromCache }) => {
          if (config) {
            setAppConfig(config)
            if (!config.has_users) setMode('register')
            // Skip auto-redirect when config is from cache — network is unreliable
            // and auto-redirecting to the IdP could loop if the proxy changed.
            // Not in the app: its WebView cannot host the IdP, and opening the
            // system browser uninvited on every launch would be worse than a button.
            if (allowRedirect && !fromCache && !nativeApp && redirectsToIdp(config) && config.has_users && !invite && !noRedirect) {
              setIdpRedirect(true)
              window.location.href = IDP_LOGIN_URL
            }
          }
          setConfigLoaded(true)
        })
    }

    if (invite) {
      setInviteToken(invite)
      setMode('register')
      authApi.validateInvite(invite).then(() => {
        setInviteValid(true)
      }).catch(() => {
        setError(t('login.invalidInviteLink'))
      })
      window.history.replaceState({}, '', window.location.pathname)
    }

    if (oidcCode) {
      exchangeInitiated.current = true
      setIsLoading(true)
      fetch('/api/auth/oidc/exchange?code=' + encodeURIComponent(oidcCode), { credentials: 'include' })
        .then(async r => ({ ok: r.ok, data: (await r.json()) as { success?: boolean; error?: string } }))
        .then(async ({ ok, data }) => {
          window.history.replaceState({}, '', '/login')
          // The exchange set the session cookie; the deprecated `token` in the
          // body is never read, so it can go without breaking SSO sign-in.
          if (ok && data.success === true) {
            await loadUser()
            // Taken either way, so a handoff left from an abandoned sign-in cannot catch a later one.
            const handoffReturn = takeHandoffReturn()
            const savedRedirect = sessionStorage.getItem('oidc_redirect') || handoffReturn || START_DESTINATION_ROUTE
            sessionStorage.removeItem('oidc_redirect')
            navigate(savedRedirect, { replace: true })
          } else {
            setError(data.error || t('login.oidcFailed'))
            loadConfig(false)
          }
        })
        .catch(() => {
          window.history.replaceState({}, '', '/login')
          setError(t('login.oidcFailed'))
          loadConfig(false)
        })
        .finally(() => setIsLoading(false))
      return
    }

    if (oidcError) {
      const errorMessages: Record<string, string> = {
        registration_disabled: t('login.oidc.registrationDisabled'),
        no_email: t('login.oidc.noEmail'),
        token_failed: t('login.oidc.tokenFailed'),
        invalid_state: t('login.oidc.invalidState'),
        email_not_verified: t('login.oidc.emailNotVerified'),
        email_link_blocked: t('login.oidc.emailLinkBlocked'),
      }
      setError(errorMessages[oidcError] || oidcError)
      // A round trip the app's sign-in started keeps its way back. The next SSO
      // attempt from this page is a plain web login, and without the handoff page
      // to return to it ends in the web app inside the app's browser sheet. The
      // handoff page asks for a tap before it passes anything on, so keeping the
      // path hands nothing over by itself; the copy is renewed for a slow retry.
      const stashed = sessionStorage.getItem('oidc_redirect')
      if (isHandoffReturn(stashed)) rememberHandoffReturn(stashed)
      else sessionStorage.removeItem('oidc_redirect')
      window.history.replaceState({}, '', '/login')
      loadConfig(false)
      return
    }

    loadConfig(true)
  }, [navigate, t, noRedirect, nativeApp])

  // Language detection chain (runs once on mount, only if user has no saved preference):
  // 1. localStorage → already in store initial state, skip
  // 2. Browser/OS language (navigator.languages)
  // 3. Server default (DEFAULT_LANGUAGE env var)
  // 4. 'en' → hardcoded fallback already in store
  // Inside the iOS and Android app the sign-in screen has no language menu: unless
  // a language was chosen before, it speaks the device's language, as the address
  // screen before it does, and English where TREK has no translation for it. The
  // account's own language takes over once signed in.
  useEffect(() => {
    if (hasStoredLanguage()) return
    if (nativeApp) {
      setLanguageTransient(detectBrowserLanguage() ?? 'en')
      return
    }

    const detected = detectBrowserLanguage()
    if (detected) {
      setLanguageTransient(detected)
      return
    }

    configApi.getPublicConfig()
      .then(({ defaultLanguage }) => { if (defaultLanguage) setLanguageTransient(defaultLanguage) })
      .catch((err) => console.warn('Failed to fetch default language config:', err))
  }, [setLanguageTransient, nativeApp])

  useEffect(() => {
    if (!langDropdownOpen) return
    const close = () => setLangDropdownOpen(false)
    document.addEventListener('click', close)
    return () => document.removeEventListener('click', close)
  }, [langDropdownOpen])

  const handleDemoLogin = async (): Promise<void> => {
    setError('')
    setIsLoading(true)
    try {
      await demoLogin()
      takeOff()
    } catch (err: unknown) {
      setError(err instanceof Error ? err.message : t('login.demoFailed'))
    } finally {
      setIsLoading(false)
    }
  }

  // In the app: a sign-in the system browser finished while Android started the
  // app anew. Its return waited natively; redeemed here, it signs the app in.
  useEffect(() => {
    if (!nativeApp) return
    resumeBrowserSignIn()
      .then(async (signedIn) => {
        if (!signedIn) return
        await loadUser({ silent: true })
        setShowTakeoff(true)
        toastHaptic('success')
        void handOverToApp(loadSettings(), () => {
          void navigate(redirectTarget)
        })
      })
      .catch(() => setError(t('native.login.failed')))
  }, [nativeApp, loadUser, loadSettings, navigate, redirectTarget, t])

  /** In the app: SSO and passkeys run in the system browser (native/browserSignIn.ts). */
  const handleBrowserSignIn = async (start?: BrowserSignInStart): Promise<void> => {
    setError('')
    setIsLoading(true)
    try {
      await signInThroughBrowser(start)
      await loadUser({ silent: true })
      takeOff()
    } catch (err: unknown) {
      if (!(err instanceof BrowserSignInCancelled)) setError(t('native.login.failed'))
      setIsLoading(false)
    }
  }

  /** The SSO links: a plain navigation in a browser, the system browser in the app. */
  const handleSsoClick = (e: React.MouseEvent<HTMLAnchorElement>): void => {
    clearSignedOut()
    if (!nativeApp) return
    e.preventDefault()
    void handleBrowserSignIn('oidc')
  }

  const handlePasskeyLogin = async (): Promise<void> => {
    if (nativeApp) return handleBrowserSignIn('passkey')
    setError('')
    setIsLoading(true)
    try {
      const options = await authApi.passkey.loginOptions()
      const assertion = await startAuthentication({ optionsJSON: options })
      await authApi.passkey.loginVerify(assertion)
      await loadUser({ silent: true })
      takeOff()
    } catch (err: unknown) {
      // The user dismissing the native prompt isn't an error worth surfacing.
      const name = (err as { name?: string })?.name
      if (name === 'NotAllowedError' || name === 'AbortError') {
        setIsLoading(false)
        return
      }
      setError(getApiErrorMessage(err, t('login.passkey.failed')))
      setIsLoading(false)
    }
  }

  const handleSubmit = async (e: React.FormEvent<HTMLFormElement>): Promise<void> => {
    e.preventDefault()
    setError('')
    setInsecureCookie(false)
    setIsLoading(true)
    try {
      if (passwordChangeStep) {
        if (!newPassword) { setError(t('settings.passwordRequired')); setIsLoading(false); return }
        const weak = passwordErrorKey(newPassword)
        if (weak) { setError(t(weak)); setIsLoading(false); return }
        if (newPassword !== confirmPassword) { setError(t('settings.passwordMismatch')); setIsLoading(false); return }
        await authApi.changePassword({ current_password: savedLoginPassword, new_password: newPassword })
        await loadUser({ silent: true })
        takeOff()
        return
      }
      if (mode === 'login' && mfaStep) {
        if (!mfaCode.trim()) {
          setError(t('login.mfaCodeRequired'))
          setIsLoading(false)
          return
        }
        const mfaResult = await completeMfaLogin(mfaToken, mfaCode, rememberMe)
        if ('user' in mfaResult && mfaResult.user?.must_change_password) {
          setSavedLoginPassword(password)
          setPasswordChangeStep(true)
          setIsLoading(false)
          return
        }
        takeOff()
        return
      }
      if (mode === 'register') {
        if (!username.trim()) { setError(t('login.usernameRequired')); setIsLoading(false); return }
        const weakPassword = passwordErrorKey(password)
        if (weakPassword) { setError(t(weakPassword)); setIsLoading(false); return }
        await register(username, email, password, inviteToken || undefined)
      } else {
        const result = await login(email, password, rememberMe)
        if ((result as { insecureCookie?: boolean }).insecureCookie) {
          // Credentials were correct, but the secure cookie won't survive plain
          // HTTP — proceeding would just dead-end on "Access token required".
          setInsecureCookie(true)
          setIsLoading(false)
          return
        }
        if ('mfa_required' in result && result.mfa_required && 'mfa_token' in result) {
          setMfaToken(result.mfa_token)
          setMfaStep(true)
          setMfaCode('')
          setIsLoading(false)
          return
        }
        if ('user' in result && result.user?.must_change_password) {
          setSavedLoginPassword(password)
          setPasswordChangeStep(true)
          setIsLoading(false)
          return
        }
      }
      takeOff()
    } catch (err: unknown) {
      setError(getApiErrorMessage(err, t('login.error')))
      setIsLoading(false)
    }
  }

  const showRegisterOption = (appConfig?.password_registration || !appConfig?.has_users || inviteValid) && (appConfig?.setup_complete !== false || !appConfig?.has_users)

  // In OIDC-only mode, show a minimal page that redirects directly to the IdP
  const oidcOnly = !appConfig?.password_login && appConfig?.oidc_login && appConfig?.oidc_configured

  return {
    navigate, nativeApp,
    mode, setMode,
    username, setUsername, email, setEmail, password, setPassword, rememberMe, setRememberMe, showPassword, setShowPassword,
    isLoading, error, setError, insecureCookie, appConfig, inviteToken,
    langDropdownOpen, setLangDropdownOpen, setLanguageLocal,
    showTakeoff, mfaStep, setMfaStep, mfaToken, setMfaToken, mfaCode, setMfaCode,
    passwordChangeStep, newPassword, setNewPassword, confirmPassword, setConfirmPassword,
    noRedirect, showRegisterOption, oidcOnly,
    redirectScreen, configWait, idpSlow, idpName,
    handleDemoLogin, handleSubmit, handlePasskeyLogin,
    handleSsoClick,
  }
}