import { Button } from "@/components/Button"; import { Input } from "@/components/common/Input"; import { Text } from "@/components/common/Text"; 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 { KeyboardAvoidingView, Platform, 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 { 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 | z.ZodError; if (e instanceof z.ZodError) { setError("An error occured."); } else { if (e.response?.status === 401) { setError("Invalid credentials."); } else { setError( "A network error occurred. Did you enter the correct server URL?", ); } } } finally { setLoading(false); } }; const handleConnect = (url: string) => { setServer({ address: url.trim() }); }; if (api?.basePath) { return ( Streamyfin Server: {api.basePath} Log in 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 Enter a server adress ); }; export default Login;