Fix password handle

This commit is contained in:
mbaharip
2024-04-07 00:35:16 +07:00
parent 1af73e9e15
commit b645eeb1b8
4 changed files with 24 additions and 7 deletions
+3 -1
View File
@@ -3,7 +3,7 @@
import { TooltipTrigger } from "@radix-ui/react-tooltip";
import { useTheme } from "next-themes";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import toast from "react-hot-toast";
import { cn } from "~/utils";
@@ -52,6 +52,7 @@ import { ClearPassword } from "./actions";
export default function Navbar() {
const pathname = usePathname();
const router = useRouter();
const isDesktop = useMediaQuery("(min-width: 768px)");
const { theme, themes, setTheme } = useTheme();
@@ -72,6 +73,7 @@ export default function Navbar() {
loading: "Clearing all saved password...",
success: () => {
setOpen(false);
router.refresh();
return "All saved password cleared successfully!";
},
error: (error) => error.message,
+16 -3
View File
@@ -9,18 +9,25 @@ import Icon from "~/components/Icon";
import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";
import { CheckSitePassword, SetPassword, SetSitePassword } from "./actions";
import {
CheckPassword,
CheckSitePassword,
SetPassword,
SetSitePassword,
} from "./actions";
type Props = {
path: string;
checkPaths?: { path: string; id: string }[];
errorMessage?: string;
};
export default function Password({ path, errorMessage }: Props) {
export default function Password({ path, checkPaths, errorMessage }: Props) {
const router = useRouter();
const [input, setInput] = useState<string>("");
const [submitLoading, setSubmitLoading] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(true);
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setSubmitLoading(true);
@@ -37,13 +44,19 @@ export default function Password({ path, errorMessage }: Props) {
const check = await CheckSitePassword();
if (!check.success) throw new Error(check.message);
} else {
if (!checkPaths)
throw new Error("No path found, try to refresh the page.");
const set = await SetPassword(path, input);
if (!set.success) throw new Error(set.message);
const check = await CheckPassword(checkPaths);
if (!check.success) throw new Error(check.message);
}
toast.success("Password accepted! Refreshing...", {
id: "password",
});
console.log("Password accepted! Refreshing...");
router.refresh();
} catch (error) {
const e = error as Error;
@@ -51,9 +64,9 @@ export default function Password({ path, errorMessage }: Props) {
toast.error(e.message, {
id: "password",
});
setSubmitLoading(false);
} finally {
setInput("");
setSubmitLoading(false);
}
};
+1
View File
@@ -88,6 +88,7 @@ export default async function RestPage({ params: { rest } }: Props) {
return (
<Password
path={unlocked.path}
checkPaths={paths.data}
errorMessage={unlocked.message}
/>
);
+4 -3
View File
@@ -256,9 +256,10 @@ export async function CheckPassword(
},
);
if (savedPassword !== passwordFile)
throw new Error(
`Password for '${currentFolder.path}' is incorrect, please re-enter the password`,
);
throw {
message: `Password for '${currentFolder.path}' is incorrect, please re-enter the password`,
path: currentFolder.id,
};
return {
success: true,