import { useState } from 'react' import { useNavigate } from 'react-router-dom' import { useAuthStore } from '../hooks/useAuth' import { useQuery } from '@tanstack/react-query' import api from '../utils/api' const AUTH_ERRORS = { not_authorized: "Your account isn't permitted to access MileVault β ask the admin to add you to the allowed group.", passkey_in_use: "That passkey is already linked to another account. Sign in to that account, or have an admin remove it on the Users page, then try linking again.", link_failed: "Couldn't link the passkey. Please try again.", invalid_state: "Your sign-in link expired or was invalid. Please try signing in again.", no_identity: "Couldn't read your identity from the provider. Please try again.", } export default function LoginPage() { const [username, setUsername] = useState('') const [password, setPassword] = useState('') const authError = new URLSearchParams(window.location.search).get('auth_error') const [error, setError] = useState(AUTH_ERRORS[authError] || '') const { login, isLoading } = useAuthStore() const navigate = useNavigate() const { data: pocketidData } = useQuery({ queryKey: ['pocketid-available'], queryFn: () => api.get('/auth/pocketid/available').then(r => r.data), }) const handleSubmit = async (e) => { e.preventDefault() setError('') try { await login(username, password) navigate('/') } catch (err) { setError(err.response?.data?.detail || 'Login failed') } } const handlePocketID = async () => { const { data } = await api.get('/auth/pocketid/login-url') window.location.href = data.url } return (
Your personal fitness dashboard