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.

3.8 KB
Raw Blame
import { useState } from 'react'
import { Text, View, Linking } from 'react-native'
import { Link } from 'expo-router'
import { AuthScaffold } from '../../src/components/AuthScaffold'
import { IconInput, GradientButton, SecondaryButton, AuthDivider, AuthError, ServerRow, Footer } from '../../src/components/authui'
import { registrationOpen, uiMessage, useTranslation, type UiMessage } from '@lyftr/shared'
import { useAuthStore, useServerInfo } from '../../src/lib/lyftr'
import { useTheme } from '../../src/theme/useTheme'

// Public hosted demo (Fly) — the "Try demo account" button opens it in the browser.
const DEMO_URL = 'https://lyftr-demo.fly.dev'

// Same intent as the web's <input type=email required>: a lightweight shape check,
// not RFC validation — the server has the final say.
const EMAIL_RE = /^\S+@\S+\.\S+$/

export default function Login() {
  const { t } = useTranslation()
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  const [localError, setLocalError] = useState<UiMessage | null>(null)
  const login = useAuthStore((s) => s.login)
  const loading = useAuthStore((s) => s.isLoading)
  const error = useAuthStore((s) => s.error)
  const clearError = useAuthStore((s) => s.clearError)
  const { accent, colors } = useTheme()
  const isOpen = registrationOpen(useServerInfo())

  // Clear both the client-side and the server error when a field is edited.
  const onChange = (setter: (t: string) => void) => (t: string) => {
    clearError()
    setLocalError(null)
    setter(t)
  }

  // Validate only on submit (not while typing): the browser's `required`/`type=email`
  // equivalent, surfaced as an error after the user presses Sign in.
  const submit = async () => {
    if (!EMAIL_RE.test(email.trim())) { setLocalError({ key: 'auth.validation.emailInvalid' }); return }
    if (!password) { setLocalError({ key: 'auth.validation.passwordRequired' }); return }
    setLocalError(null)
    try { await login(email.trim(), password) } catch {}
  }
  const demo = () => {
    Linking.openURL(DEMO_URL).catch(() => {})
  }

  const shownError = localError || error

  return (
    <AuthScaffold heading={t('auth.login.heading')} subtitle={t('auth.login.subtitle')}>
      <ServerRow />
      <IconInput
        label={t('auth.field.email')}
        icon="mail"
        value={email}
        onChangeText={onChange(setEmail)}
        keyboardType="email-address"
        placeholder={t('auth.field.emailPlaceholder')}
      />
      <IconInput
        label={t('auth.field.password')}
        icon="lock"
        password
        value={password}
        onChangeText={onChange(setPassword)}
        placeholder="••••••••"
      />
      {shownError ? <AuthError message={uiMessage(shownError)} /> : null}
      <GradientButton title={t('auth.login.submit')} onPress={submit} loading={loading} />
      <AuthDivider />
      <SecondaryButton title={t('auth.login.demo')} hint={t('auth.login.demoHint')} onPress={demo} />
      {/* A closed server says so instead of offering a link that 403s on submit — but it
          does still say so, rather than leaving someone who came to sign up hunting for a
          button that is not there (mastodon/mastodon#21556). */}
      <Footer>
        {isOpen ? (
          <View style={{ flexDirection: 'row', gap: 5 }}>
            <Text style={{ color: colors.txSecondary, fontFamily: 'PlusJakartaSans_600SemiBold', fontSize: 14 }}>{t('auth.login.newHere')}</Text>
            <Link href="/register" style={{ color: accent, fontFamily: 'PlusJakartaSans_800ExtraBold', fontSize: 14 }}>
              {t('auth.login.createAccountLink')}
            </Link>
          </View>
        ) : (
          <Text style={{ color: colors.txSecondary, fontFamily: 'PlusJakartaSans_600SemiBold', fontSize: 14 }}>
            {t('auth.closed.notice')}
          </Text>
        )}
      </Footer>
    </AuthScaffold>
  )
}