import { useCallback, useEffect, useState } from "react"; import ActivityIndicator from "@/components/indicators/activity-indicator"; import { FrigateConfig } from "@/types/frigateConfig"; import { Toaster } from "@/components/ui/sonner"; import useSWR from "swr"; import Heading from "@/components/ui/heading"; import { User } from "@/types/user"; import { Button } from "@/components/ui/button"; import SetPasswordDialog from "@/components/overlay/SetPasswordDialog"; import axios from "axios"; import CreateUserDialog from "@/components/overlay/CreateUserDialog"; import { toast } from "sonner"; import DeleteUserDialog from "@/components/overlay/DeleteUserDialog"; import { Card } from "@/components/ui/card"; import { HiTrash } from "react-icons/hi"; import { FaUserEdit } from "react-icons/fa"; import { LuPlus } from "react-icons/lu"; export default function AuthenticationView() { const { data: config } = useSWR("config"); const { data: users, mutate: mutateUsers } = useSWR("users"); const [showSetPassword, setShowSetPassword] = useState(false); const [showCreate, setShowCreate] = useState(false); const [showDelete, setShowDelete] = useState(false); const [selectedUser, setSelectedUser] = useState(); useEffect(() => { document.title = "Authentication Settings - Frigate"; }, []); const onSavePassword = useCallback((user: string, password: string) => { axios .put(`users/${user}/password`, { password: password, }) .then((response) => { if (response.status == 200) { setShowSetPassword(false); } }) .catch((_error) => { toast.error("Error setting password", { position: "top-center", }); }); }, []); const onCreate = async (user: string, password: string) => { try { await axios.post("users", { username: user, password: password, }); setShowCreate(false); mutateUsers((users) => { users?.push({ username: user }); return users; }, false); } catch (error) { toast.error("Error creating user. Check server logs.", { position: "top-center", }); } }; const onDelete = async (user: string) => { try { await axios.delete(`users/${user}`); setShowDelete(false); mutateUsers((users) => { return users?.filter((u) => { return u.username !== user; }); }, false); } catch (error) { toast.error("Error deleting user. Check server logs.", { position: "top-center", }); } }; if (!config || !users) { return ; } return (
Users
{users.map((u) => (
{u.username}
))}
{ setShowSetPassword(false); }} onSave={(password) => { onSavePassword(selectedUser!, password); }} /> { setShowDelete(false); }} onDelete={() => { onDelete(selectedUser!); }} /> { setShowCreate(false); }} />
); }