From 2fe83b4209f7dc07f988a46326b213e76c14c414 Mon Sep 17 00:00:00 2001 From: Fredrik Burmester Date: Sun, 11 Aug 2024 12:36:17 +0200 Subject: [PATCH] chore: file --- app/login.tsx | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 app/login.tsx diff --git a/app/login.tsx b/app/login.tsx new file mode 100644 index 00000000..b5a6dc86 --- /dev/null +++ b/app/login.tsx @@ -0,0 +1,166 @@ +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 { 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"), + password: z.string().min(1, "Password is required"), +}); + +const Login: React.FC = () => { + const { setServer, login, removeServer } = useJellyfin(); + const [api] = useAtom(apiAtom); + + const [serverURL, setServerURL] = 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) { + console.error(error); + } finally { + setLoading(false); + } + }; + + const parsedServerURL = useMemo(() => { + let parsedServerURL = serverURL.trim(); + + if (parsedServerURL) { + parsedServerURL = parsedServerURL.endsWith("/") + ? parsedServerURL.replace("/", "") + : parsedServerURL; + parsedServerURL = parsedServerURL.startsWith("http") + ? parsedServerURL + : "http://" + parsedServerURL; + + return parsedServerURL; + } + + return ""; + }, [serverURL]); + + const handleConnect = (url: string) => { + setServer({ address: url }); + }; + + if (api?.basePath) { + return ( + + + + Jellyfin + 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} + /> + + + + + + ); + } + + return ( + + + + Jellyfin + Enter a server adress + + + + + + ); +}; + +export default Login;