'use client' import { useRouter } from 'next/navigation' import { useState } from 'react' import { getApiBase, setToken, clearToken } from '../lib/auth' import BrandingLogo from '../ui/BrandingLogo' export default function LoginPage() { const router = useRouter() const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState(null) const [loading, setLoading] = useState(false) const submit = async (event: React.FormEvent, mode: 'local' | 'jellyfin') => { event.preventDefault() setError(null) setLoading(true) try { clearToken() const baseUrl = getApiBase() const endpoint = mode === 'jellyfin' ? '/auth/jellyfin/login' : '/auth/login' const body = new URLSearchParams({ username, password }) const response = await fetch(`${baseUrl}${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body, }) if (!response.ok) { throw new Error('Login failed') } const data = await response.json() if (data?.access_token) { setToken(data.access_token) if (typeof window !== 'undefined') { window.location.href = '/' return } router.push('/') return } throw new Error('Login failed') } catch (err) { console.error(err) setError('Invalid username or password.') } finally { setLoading(false) } } return (

Sign in

Use your Jellyfin account, or sign in with Magent instead.

submit(event, 'jellyfin')} className="auth-form"> {error &&
{error}
}
) }