import { useRef, useState } from "react"; import { Keyboard, KeyboardAvoidingView, Pressable, TouchableWithoutFeedback, View, } from "react-native"; import { Redirect, useRouter } from "expo-router"; import { CustomHeadersModal } from "@/components/CustomHeadersModal"; import Logo from "@/components/Logo"; import { TailwindResolver } from "@/components/TailwindResolver"; import { Button } from "@/components/ui/Button"; import { Input } from "@/components/ui/Input"; import { Text } from "@/components/ui/Text"; import useAppSettings from "@/lib/settings"; import { api } from "@/lib/trpc"; import { Bug, Check, Edit3 } from "lucide-react-native"; enum LoginType { Password, ApiKey, } export default function Signin() { const { settings, setSettings } = useAppSettings(); const router = useRouter(); const [error, setError] = useState(); const [loginType, setLoginType] = useState(LoginType.Password); const [isEditingServerAddress, setIsEditingServerAddress] = useState(false); const [tempServerAddress, setTempServerAddress] = useState( settings.address ?? "https://cloud.karakeep.app", ); const [isCustomHeadersModalVisible, setIsCustomHeadersModalVisible] = useState(false); const emailRef = useRef(""); const passwordRef = useRef(""); const apiKeyRef = useRef(""); const toggleLoginType = () => { setLoginType((prev) => { if (prev === LoginType.Password) { return LoginType.ApiKey; } else { return LoginType.Password; } }); }; const { mutate: login, isPending: userNamePasswordRequestIsPending } = api.apiKeys.exchange.useMutation({ onSuccess: (resp) => { setSettings({ ...settings, apiKey: resp.key, apiKeyId: resp.id }); }, onError: (e) => { if (e.data?.code === "UNAUTHORIZED") { setError("Wrong username or password"); } else { setError(`${e.message}`); } }, }); const { mutate: validateApiKey, isPending: apiKeyValueRequestIsPending } = api.apiKeys.validate.useMutation({ onSuccess: () => { const apiKey = apiKeyRef.current; setSettings({ ...settings, apiKey: apiKey }); }, onError: (e) => { if (e.data?.code === "UNAUTHORIZED") { setError("Invalid API key"); } else { setError(`${e.message}`); } }, }); if (settings.apiKey) { return ; } const handleSaveCustomHeaders = (headers: Record) => { setSettings({ ...settings, customHeaders: headers }); }; const onSignin = () => { if (!tempServerAddress) { setError("Server address is required"); return; } if ( !tempServerAddress.startsWith("http://") && !tempServerAddress.startsWith("https://") ) { setError("Server address must start with http:// or https://"); return; } if (loginType === LoginType.Password) { const email = emailRef.current; const password = passwordRef.current; const randStr = (Math.random() + 1).toString(36).substring(5); login({ email: email.trim(), password: password, keyName: `Mobile App: (${randStr})`, }); } else if (loginType === LoginType.ApiKey) { const apiKey = apiKeyRef.current; validateApiKey({ apiKey: apiKey }); } }; return ( ( )} /> {error && ( {error} )} Server Address {!isEditingServerAddress ? ( {tempServerAddress} ) : ( )} setIsCustomHeadersModalVisible(true)} className="mt-1" > Configure Custom Headers{" "} {settings.customHeaders && Object.keys(settings.customHeaders).length > 0 && `(${Object.keys(settings.customHeaders).length})`} {loginType === LoginType.Password && ( <> Email (emailRef.current = text)} /> Password (passwordRef.current = text)} /> )} {loginType === LoginType.ApiKey && ( API Key (apiKeyRef.current = text)} /> )} {loginType === LoginType.Password ? "Use API key instead?" : "Use password instead?"} setIsCustomHeadersModalVisible(false)} onSave={handleSaveCustomHeaders} /> ); }