import { Button } from "@/components/Button"; import { Input } from "@/components/common/Input"; import { Text } from "@/components/common/Text"; import { LanguageSwitcher } from "@/components/LanguageSwitcher"; import { apiAtom, useJellyfin } from "@/providers/JellyfinProvider"; import { Ionicons } from "@expo/vector-icons"; import { AxiosError } from "axios"; import { useAtom } from "jotai"; import React, { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { Alert, KeyboardAvoidingView, Platform, SafeAreaView, View, } from "react-native"; import { z } from "zod"; const CredentialsSchema = z.object({ username: z.string().min(1, "Username is required"), }); const Login: React.FC = () => { const { t, i18n } = useTranslation(); const { setServer, login, removeServer } = useJellyfin(); const [api] = useAtom(apiAtom); const [serverURL, setServerURL] = useState(""); const [error, setError] = useState(""); const [credentials, setCredentials] = useState<{ username: string; password: string; }>({ username: "", password: "", }); const [loading, setLoading] = useState(false); const handleLogin = async () => { setLoading(true); try { const result = CredentialsSchema.safeParse(credentials); if (result.success) { await login(credentials.username, credentials.password); } } catch (error) { const e = error as AxiosError; setError(e.message); } finally { setLoading(false); } }; const handleConnect = (url: string) => { if (!url.startsWith("http")) { Alert.alert("Error", "URL needs to start with http or https."); return; } setServer({ address: url.trim() }); }; if (api?.basePath) { return ( Streamyfin {t("server.server_label", { serverURL: api.basePath })} {t("login.login")} {t("login.login_subtitle")} setCredentials({ ...credentials, username: text }) } value={credentials.username} autoFocus secureTextEntry={false} keyboardType="default" returnKeyType="done" autoCapitalize="none" textContentType="username" clearButtonMode="while-editing" maxLength={500} /> setCredentials({ ...credentials, password: text }) } value={credentials.password} secureTextEntry keyboardType="default" returnKeyType="done" autoCapitalize="none" textContentType="password" clearButtonMode="while-editing" maxLength={500} /> {error} ); } return ( Streamyfin {t("server.connect_to_server")} {t("server.server_url_hint")} ); }; export default Login;