Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
6.7 KB
Raw Blame
import type { FormEvent } from 'react'
import { useState } from 'react'
import { useAuthContext } from '@contexts/AuthContext'
import { AuthBrand } from '@components/AuthBrand'
import { Button, Field, StatusBanner, TextInput } from '@components/Common'
import { RuntimeConfigDock } from '@components/RuntimeConfigDock'

/**
 * Sign-in for a configured environment.
 *
 * A cold install never reaches this page — the unauthenticated branch renders
 * onboarding instead, so the invitation email form that used to
 * live here now belongs to the wizard's invited step. AuthPage has
 * exactly one mode again: sign in, plus the environment dock and its inline
 * add-environment form.
 */
export function AuthPage() {
  const {
    busy,
    email,
    password,
    runtimeConfigSetupName,
    runtimeConfigSetupExternalRestApiBaseUrl,
    authTransitioning,
    backendSwitchHint,
    setEmail,
    setPassword,
    setRuntimeConfigSetupName,
    setRuntimeConfigSetupExternalRestApiBaseUrl,
    setStatus,
    handlePasswordLogin,
    handleSwitchLoginBackend,
    handleSaveRuntimeConfig,
  } = useAuthContext()

  const hasPasswordLoginCredentials = Boolean(email.trim()) && Boolean(password)
  const [runtimeSetupVisible, setRuntimeSetupVisible] = useState(false)
  const hasRuntimeSetupValues =
    Boolean(runtimeConfigSetupName.trim()) &&
    Boolean(runtimeConfigSetupExternalRestApiBaseUrl.trim())
  const [forgotPasswordBusy, setForgotPasswordBusy] = useState(false)

  const handlePasswordSubmit = (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    if (!busy && hasPasswordLoginCredentials) {
      handlePasswordLogin()
    }
  }

  const handleForgotPasswordClick = async () => {
    if (busy || forgotPasswordBusy) return
    setForgotPasswordBusy(true)
    try {
      await window.clerum.auth.openForgotPassword(email.trim().toLowerCase())
      setStatus('Password reset opened in your browser.', 'success')
    } catch {
      setStatus('Password reset could not be opened.', 'error')
    } finally {
      setForgotPasswordBusy(false)
    }
  }

  const handleRuntimeSetupSubmit = (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    if (!busy && hasRuntimeSetupValues) {
      handleSaveRuntimeConfig().then(state => {
        if (state) setRuntimeSetupVisible(false)
      })
    }
  }

  const handleRuntimeSetupCancel = () => {
    setRuntimeSetupVisible(false)
  }

  return (
    <main className="auth-page">
      <section className="auth-card glass-card">
        <header className="auth-card__header">
          <AuthBrand />
        </header>

        {/* Sign-in stays content-sized. The fixed height belongs to the
            onboarding wizard, which needs it because its steps differ wildly
            in length; sign-in has one short form and reads better snug. */}
        {runtimeSetupVisible ? (
          <form className="auth-form-stack" onSubmit={handleRuntimeSetupSubmit}>
            <Field
              label="Environment name"
              htmlFor="runtime-config-name-input"
              wrapperClassName="auth-form-row"
            >
              <TextInput
                id="runtime-config-name-input"
                type="text"
                placeholder="Production"
                value={runtimeConfigSetupName}
                onChange={event => setRuntimeConfigSetupName(event.target.value)}
              />
            </Field>
            <Field
              label="External REST API"
              htmlFor="runtime-config-external-rest-api-input"
              wrapperClassName="auth-form-row"
            >
              <TextInput
                id="runtime-config-external-rest-api-input"
                type="url"
                placeholder="https://example.com"
                value={runtimeConfigSetupExternalRestApiBaseUrl}
                onChange={event => setRuntimeConfigSetupExternalRestApiBaseUrl(event.target.value)}
              />
            </Field>
            <Button block disabled={!hasRuntimeSetupValues || busy} type="submit">
              Save environment
            </Button>
            <button
              type="button"
              className="auth-inline-link"
              onClick={handleRuntimeSetupCancel}
              disabled={busy}
            >
              Go back to login
            </button>
          </form>
        ) : (
          <form className="auth-form-stack" onSubmit={handlePasswordSubmit}>
            <Field label="Email" htmlFor="email-input" wrapperClassName="auth-form-row">
              <TextInput
                id="email-input"
                type="email"
                placeholder="[email protected]"
                value={email}
                onChange={event => setEmail(event.target.value)}
              />
            </Field>
            <Field label="Password" htmlFor="password-input" wrapperClassName="auth-form-row">
              <TextInput
                id="password-input"
                type="password"
                placeholder="Your password"
                value={password}
                onChange={event => setPassword(event.target.value)}
              />
            </Field>
            <Button block disabled={!hasPasswordLoginCredentials || busy} type="submit">
              {authTransitioning ? (
                <span className="auth-button-loading">
                  <span className="auth-button-spinner" aria-hidden="true" />
                  Signing in...
                </span>
              ) : (
                'Sign in'
              )}
            </Button>
            <button
              type="button"
              className="auth-inline-link"
              onClick={() => void handleForgotPasswordClick()}
              disabled={busy || forgotPasswordBusy}
            >
              Forgot password
            </button>
            {backendSwitchHint ? (
              <StatusBanner tone="warn">
                <span className="auth-backend-hint">
                  <span>
                    Couldn’t reach <strong>{backendSwitchHint.activeLabel}</strong>. A local
                    Evenfire looks like it’s running — switch to{' '}
                    <strong>{backendSwitchHint.targetLabel}</strong> and retry?
                  </span>
                  <Button
                    type="button"
                    size="sm"
                    variant="soft"
                    block
                    disabled={busy}
                    onClick={() => void handleSwitchLoginBackend()}
                  >
                    {`Switch to ${backendSwitchHint.targetLabel} & retry`}
                  </Button>
                </span>
              </StatusBanner>
            ) : null}
          </form>
        )}
      </section>
      <RuntimeConfigDock onAddEnvironment={() => setRuntimeSetupVisible(true)} />
    </main>
  )
}