Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

8.2 KB
Raw Blame
import { useState } from 'react'
import { useNavigate, Link } from 'react-router-dom'
import { AlertCircle, Zap, Dumbbell, Apple, TrendingUp, LogIn } from 'lucide-react'
import { useAuthStore } from '../stores/auth'
import { useServerInfo } from '../hooks/useServerInfo'
import { formatVersion, registrationOpen, demoMode, uiMessage, useTranslation, type UiMessage } from '@lyftr/shared'
import Logo from '../components/Logo'
import ServerSettings from '../components/ServerSettings'
import PasswordField from '../components/ui/PasswordField'

export default function Login() {
  const { t } = useTranslation()
  const navigate = useNavigate()
  const { login } = useAuthStore()
  const serverInfo = useServerInfo()

  const [email, setEmail]           = useState('')
  const [password, setPassword]     = useState('')
  const [error, setError]           = useState<UiMessage | null>(null)
  const [isLoading, setLoading]     = useState(false)

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    setError(null)
    setLoading(true)
    try {
      await login(email, password)
      navigate('/')
    } catch (err: any) {
      setError({ key: 'auth.login.failed', cause: err })
    } finally {
      setLoading(false)
    }
  }

  const handleDemoLogin = async () => {
    setLoading(true)
    try {
      await login('[email protected]', 'password123')
      navigate('/')
    } catch (err: any) {
      setError({ key: 'auth.login.demoFailed', cause: err })
    } finally {
      setLoading(false)
    }
  }

  return (
    <div className="min-h-screen grid grid-cols-1 lg:grid-cols-2 bg-surface-base">
      {/* Left side — branding */}
      <div className="hidden lg:flex flex-col justify-between p-12 relative overflow-hidden" style={{
        background: 'linear-gradient(135deg, #030812 0%, #0a1b2e 50%, #081326 100%)',
      }}>
        {/* Gradient overlays */}
        <div className="absolute inset-0 pointer-events-none"
          style={{
            background: `
              radial-gradient(ellipse at 20% 30%, rgba(0, 184, 217, 0.25) 0%, transparent 50%),
              radial-gradient(ellipse at 80% 70%, rgba(139, 92, 246, 0.15) 0%, transparent 50%)
            `,
          }}
        />

        {/* Logo */}
        <div className="relative">
          <Logo size="md" />
        </div>

        {/* Headline and features */}
        <div className="relative space-y-8">
          <h1 className="font-display font-bold text-5xl leading-tight tracking-tight">
            {t('auth.hero.headline')}
            <br />
            <span className="bg-gradient-to-r from-brand-500 to-violet-500 bg-clip-text text-transparent">
              {t('auth.hero.headlineAccent')}
            </span>
          </h1>

          <p className="text-tx-secondary text-base leading-relaxed max-w-sm">
            {t('auth.hero.description')}
          </p>

          {/* Features */}
          <div className="space-y-4">
            {([
              { icon: Dumbbell, label: 'auth.hero.features.workouts' },
              { icon: Apple, label: 'auth.hero.features.food' },
              { icon: TrendingUp, label: 'auth.hero.features.progress' },
            ] as const).map(({ icon: Icon, label }) => (
              <div key={label} className="flex items-center gap-3 text-tx-muted text-sm">
                <Icon className="w-4 h-4 text-brand-500" strokeWidth={2} />
                {t(label)}
              </div>
            ))}
          </div>
        </div>

        {/* Footer */}
        <div className="relative text-tx-muted text-xs">
          © lyftr{serverInfo?.version ? ` · ${formatVersion(serverInfo.version)}` : ''}
        </div>
      </div>

      {/* Right side — form */}
      <div className="flex items-center justify-center p-6 lg:p-12">
        <div className="w-full max-w-sm">
          {/* Mobile logo */}
          <div className="lg:hidden mb-8">
            <Logo size="lg" />
          </div>

          {/* Heading */}
          <div className="mb-8">
            <h2 className="font-display font-bold text-3xl text-tx-primary tracking-tight">
              {t('auth.login.heading')}
            </h2>
            <p className="text-tx-muted text-sm mt-2">
              {t('auth.login.subtitle')}
            </p>
          </div>

          {/* Server selector */}
          <ServerSettings />

          {/* Form */}
          <form onSubmit={handleSubmit} className="space-y-4">
            {/* Email */}
            <div>
              <label htmlFor="email" className="label">{t('auth.field.email')}</label>
              <input
                id="email"
                type="email"
                value={email}
                onChange={e => setEmail(e.target.value)}
                className="input mt-2"
                placeholder={t('auth.field.emailPlaceholder')}
                autoComplete="email"
                required
              />
            </div>

            {/* Password */}
            <PasswordField
              id="password"
              label={t('auth.field.password')}
              value={password}
              onChange={setPassword}
              autoComplete="current-password"
              placeholder="••••••••"
            />

            {/* Error */}
            {error && (
              <div className="alert-error">
                <AlertCircle className="w-4 h-4 mt-0.5 flex-shrink-0" />
                <span>{uiMessage(error)}</span>
              </div>
            )}

            {/* Sign in button */}
            <button
              type="submit"
              disabled={isLoading}
              className="btn-primary btn-lg w-full mt-6 flex items-center justify-center gap-2"
            >
              <LogIn className="w-4 h-4" />
              {isLoading ? t('auth.login.submitting') : t('auth.login.submit')}
            </button>

            {/* Divider — only when something follows it that is genuinely an alternative
                way in. Where there is no demo account and registration is closed there is
                nothing below but a statement, and an "or" heading it reads like the start
                of an option that never arrives. */}
            {(demoMode(serverInfo) || registrationOpen(serverInfo)) && (
              <div className="relative flex items-center my-6">
                <div className="flex-1 h-px bg-surface-border" />
                <span className="px-3 text-xs text-tx-muted uppercase tracking-wider">{t('auth.divider.orLower')}</span>
                <div className="flex-1 h-px bg-surface-border" />
              </div>
            )}

            {/* Gated on the server, not on import.meta.env.DEV, which is decided at build
                time and so was wrong both ways: absent from the public demo (a production
                build) and present against a DEMO_MODE=off server, where it 401s. DemoMode is
                the same flag that seeds the account, so the button follows the account. */}
            {demoMode(serverInfo) && (
              <button
                type="button"
                onClick={handleDemoLogin}
                disabled={isLoading}
                className="btn-secondary btn-lg w-full flex items-center justify-center gap-2"
              >
                <Zap className="w-4 h-4 text-warning-400" />
                {t('auth.login.demo')}
              </button>
            )}
          </form>

          {/* A closed instance says so rather than offering a link that 403s on submit —
              but it does still say so. Removing the signup path without a trace sends
              someone who came here to sign up hunting for a button that is not there
              (mastodon/mastodon#21556); a line of text answers them where they are. */}
          {registrationOpen(serverInfo) ? (
            <p className="mt-8 text-center text-sm text-tx-muted">
              {t('auth.login.newHere')}{' '}
              <Link
                to="/register"
                className="text-brand-400 font-medium hover:text-brand-300 transition-colors"
              >
                {t('auth.login.createAccountLink')}
              </Link>
            </p>
          ) : (
            <p className="mt-8 text-center text-sm text-tx-muted">
              {t('auth.closed.notice')}
            </p>
          )}
        </div>
      </div>
    </div>
  )
}