import { useMutation, useQuery } from "@tanstack/react-query"; import React, { type ChangeEvent, type FormEvent, useEffect, useState, } from "react"; import { saveUser } from "@/utils/storage/user.js"; import LoadingButton from "@mui/lab/LoadingButton"; import { OutlinedInput, Paper, TextField, Typography } from "@mui/material"; import Button from "@mui/material/Button"; import Checkbox from "@mui/material/Checkbox"; import FormControl from "@mui/material/FormControl"; import FormControlLabel from "@mui/material/FormControlLabel"; import IconButton from "@mui/material/IconButton"; import InputAdornment from "@mui/material/InputAdornment"; import InputLabel from "@mui/material/InputLabel"; import { useSnackbar } from "notistack"; import { useApiInContext } from "@/utils/store/api.js"; import { getBrandingApi } from "@jellyfin/sdk/lib/utils/api/branding-api"; import "./login.scss"; import { useBackdropStore } from "@/utils/store/backdrop.js"; import { blue } from "@mui/material/colors"; import { createFileRoute, useNavigate, useRouter, } from "@tanstack/react-router"; export const Route = createFileRoute("/_api/login/manual")({ component: UserLoginManual, validateSearch: (search): { redirect?: string } => { return { redirect: String(search.redirect) }; }, }); function UserLoginManual() { const [rememberMe, setRememberMe] = useState(true); // console.log(Route.useRouteContext()); const api = useApiInContext((s) => s.api); const createApi = useApiInContext((s) => s.createApi); const router = useRouter(); const searchParams = Route.useSearch(); const server = useQuery({ queryKey: ["login", "manual", "serverInfo"], queryFn: async () => api ? (await getBrandingApi(api).getBrandingOptions()).data : null, }); const navigate = useNavigate(); const { enqueueSnackbar } = useSnackbar(); const [username, setUsername] = useState(""); const [password, setPassword] = useState<{ password: string | undefined; showpass: boolean; }>({ showpass: false, password: "", }); const handlePassword = (prop: "password" | "showpass") => (event: ChangeEvent) => { setPassword({ ...password, [prop]: event.target.value, }); }; const handleShowPassword = () => { setPassword({ ...password, showpass: !password.showpass, }); }; const handleLoginFn = async (event: FormEvent) => { event.preventDefault(); try { if (!api) { throw new Error("Api is not available"); } const authenticate = await api.authenticateUserByName( username, password.password, ); if (!authenticate.data?.AccessToken) { throw new Error("Access token not available"); } createApi(api.basePath, authenticate.data?.AccessToken); if (rememberMe) await saveUser(username, authenticate.data.AccessToken); router.invalidate().finally(() => { navigate({ to: searchParams.redirect || "/home", replace: true }); }); } catch (err) { console.error(err); enqueueSnackbar( "Unable to authenticate. Please check your username and password", { variant: "error" }, ); } return; }; const handleLogin = useMutation({ mutationKey: ["login", "authenticate"], mutationFn: handleLoginFn, }); const handleCheckRememberMe = ( _: ChangeEvent, checked: boolean, ) => { setRememberMe(checked); }; const setBackdrop = useBackdropStore((state) => state.setBackdrop); useEffect(() => { setBackdrop("", ""); }, []); return (
Login
handleLogin.mutate(e)}> setUsername(e.currentTarget.value)} /> Password {password.showpass ? ( visibility ) : ( visibility_off )} } /> } label="Remember me" />
Login
{server.isSuccess && server.data?.LoginDisclaimer && (
info Notice
{server.data.LoginDisclaimer}
)}
); }