2024-05-18 19:36:13 +03:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import * as React from "react";
|
|
|
|
|
|
2024-08-09 16:26:26 +03:00
|
|
|
import { baseUrl } from "../../api/baseUrl";
|
2024-05-18 19:36:13 +03:00
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import ActivityIndicator from "@/components/indicators/activity-indicator";
|
|
|
|
|
import axios, { AxiosError } from "axios";
|
|
|
|
|
import { Toaster } from "@/components/ui/sonner";
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import { z } from "zod";
|
2025-03-08 19:01:08 +03:00
|
|
|
import { AuthContext } from "@/context/auth-context";
|
2025-03-16 18:36:20 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-05-18 19:36:13 +03:00
|
|
|
|
|
|
|
|
interface UserAuthFormProps extends React.HTMLAttributes<HTMLDivElement> {}
|
|
|
|
|
|
|
|
|
|
export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
|
2025-03-16 18:36:20 +03:00
|
|
|
const { t } = useTranslation(["components/auth"]);
|
2024-05-18 19:36:13 +03:00
|
|
|
const [isLoading, setIsLoading] = React.useState<boolean>(false);
|
2025-03-08 19:01:08 +03:00
|
|
|
const { login } = React.useContext(AuthContext);
|
2024-05-18 19:36:13 +03:00
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
2025-03-16 18:36:20 +03:00
|
|
|
user: z.string().min(1, t("form.errors.usernameRequired")),
|
|
|
|
|
password: z.string().min(1, t("form.errors.passwordRequired")),
|
2024-05-18 19:36:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
|
mode: "onChange",
|
2025-03-08 19:01:08 +03:00
|
|
|
defaultValues: { user: "", password: "" },
|
2024-05-18 19:36:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (values: z.infer<typeof formSchema>) => {
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
await axios.post(
|
2024-08-09 16:26:26 +03:00
|
|
|
"/login",
|
2024-05-18 19:36:13 +03:00
|
|
|
{
|
|
|
|
|
user: values.user,
|
|
|
|
|
password: values.password,
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-03-08 19:01:08 +03:00
|
|
|
headers: { "X-CSRF-TOKEN": 1 },
|
2024-05-18 19:36:13 +03:00
|
|
|
},
|
|
|
|
|
);
|
2025-03-08 19:01:08 +03:00
|
|
|
const profileRes = await axios.get("/profile", { withCredentials: true });
|
|
|
|
|
login({
|
|
|
|
|
username: profileRes.data.username,
|
|
|
|
|
role: profileRes.data.role || "viewer",
|
|
|
|
|
});
|
2024-08-09 16:26:26 +03:00
|
|
|
window.location.href = baseUrl;
|
2024-05-18 19:36:13 +03:00
|
|
|
} catch (error) {
|
|
|
|
|
if (axios.isAxiosError(error)) {
|
|
|
|
|
const err = error as AxiosError;
|
|
|
|
|
if (err.response?.status === 429) {
|
2025-03-16 18:36:20 +03:00
|
|
|
toast.error(t("form.errors.rateLimit"), {
|
2024-05-18 19:36:13 +03:00
|
|
|
position: "top-center",
|
|
|
|
|
});
|
2024-12-10 16:42:55 +03:00
|
|
|
} else if (err.response?.status === 401) {
|
2025-03-16 18:36:20 +03:00
|
|
|
toast.error(t("form.errors.loginFailed"), {
|
2024-05-18 19:36:13 +03:00
|
|
|
position: "top-center",
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2025-03-16 18:36:20 +03:00
|
|
|
toast.error(t("form.errors.unknownError"), {
|
2024-05-18 19:36:13 +03:00
|
|
|
position: "top-center",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-03-16 18:36:20 +03:00
|
|
|
toast.error(t("form.errors.webUnkownError"), {
|
2024-05-18 19:36:13 +03:00
|
|
|
position: "top-center",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn("grid gap-6", className)} {...props}>
|
|
|
|
|
<Form {...form}>
|
2025-03-10 18:00:35 +03:00
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
2024-05-18 19:36:13 +03:00
|
|
|
<FormField
|
|
|
|
|
name="user"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
2025-03-16 18:36:20 +03:00
|
|
|
<FormLabel>{t("form.user")}</FormLabel>
|
2024-05-18 19:36:13 +03:00
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
|
|
|
|
className="text-md w-full border border-input bg-background p-2 hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark]"
|
2024-06-20 19:37:54 +03:00
|
|
|
autoFocus
|
2024-05-18 19:36:13 +03:00
|
|
|
{...field}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<FormField
|
|
|
|
|
name="password"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
2025-03-16 18:36:20 +03:00
|
|
|
<FormLabel>{t("form.password")}</FormLabel>
|
2024-05-18 19:36:13 +03:00
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
|
|
|
|
className="text-md w-full border border-input bg-background p-2 hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark]"
|
|
|
|
|
type="password"
|
|
|
|
|
{...field}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<div className="flex flex-row gap-2 pt-5">
|
|
|
|
|
<Button
|
|
|
|
|
variant="select"
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
className="flex flex-1"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("form.login")}
|
2024-05-18 19:36:13 +03:00
|
|
|
>
|
|
|
|
|
{isLoading && <ActivityIndicator className="mr-2 h-4 w-4" />}
|
2025-03-16 18:36:20 +03:00
|
|
|
{t("form.login")}
|
2024-05-18 19:36:13 +03:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
<Toaster />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|