205 lines
6.1 KiB
TypeScript
205 lines
6.1 KiB
TypeScript
"use client";
|
||
|
||
import React from "react";
|
||
import { zodResolver } from "@hookform/resolvers/zod";
|
||
import { useForm } from "react-hook-form";
|
||
import * as z from "zod";
|
||
|
||
import { Button } from "@/components/ui/button";
|
||
import {
|
||
Form,
|
||
FormControl,
|
||
FormDescription,
|
||
FormField,
|
||
FormItem,
|
||
FormLabel,
|
||
FormMessage,
|
||
} from "@/components/ui/form";
|
||
|
||
import { Input } from "@/components/ui/input";
|
||
import { toast } from "@/components/ui/use-toast";
|
||
import Image from "next/image";
|
||
import LoginIcon from "../public/img/login-icon.svg";
|
||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||
import { loginStaff } from "@/utils/authenticateApi";
|
||
import { getCaptcha } from "@/utils/commonApi";
|
||
import { useRouter } from "next/navigation";
|
||
|
||
const FormSchema = z.object({
|
||
username: z.string().min(2, {
|
||
message: "Username must be at least 2 characters.",
|
||
}),
|
||
password: z.string().min(4, {
|
||
message: "Password must be at least 4 characters.",
|
||
}),
|
||
captcha: z.string().min(4, {
|
||
message: "Password must",
|
||
}),
|
||
});
|
||
|
||
export default function Login() {
|
||
const router = useRouter();
|
||
|
||
const {
|
||
data: CaptchaData,
|
||
isLoading,
|
||
refetch,
|
||
} = useQuery({
|
||
queryKey: ["Captcha"],
|
||
queryFn: () => getCaptcha(),
|
||
});
|
||
console.log(CaptchaData);
|
||
|
||
const loginMutation = useMutation({
|
||
mutationFn: loginStaff,
|
||
onSuccess: (res) => {
|
||
console.log(res);
|
||
// localStorage.setItem("roles", res.roles[0]);
|
||
// setCookie("token", res.token);
|
||
// if (res.roles[0] == "Operator") {
|
||
// router.push("/MainPage/Prescriptions");
|
||
// } else {
|
||
// router.push("/MainPage");
|
||
// }
|
||
},
|
||
onError: (err: any) => {
|
||
if (err.response.status == 428) {
|
||
router.push("/Verify");
|
||
}
|
||
},
|
||
});
|
||
|
||
const form = useForm<z.infer<typeof FormSchema>>({
|
||
resolver: zodResolver(FormSchema),
|
||
defaultValues: {
|
||
username: "",
|
||
password: "",
|
||
},
|
||
});
|
||
|
||
function onSubmit(data: z.infer<typeof FormSchema>) {
|
||
toast({
|
||
title: "You submitted the following values:",
|
||
description: (
|
||
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4 pt-12">
|
||
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
|
||
</pre>
|
||
),
|
||
});
|
||
console.log(CaptchaData?.headers["captcha-key"]);
|
||
const payload = {
|
||
username: data.username,
|
||
password: data.password,
|
||
captcha: {
|
||
key: CaptchaData?.headers["captcha-key"],
|
||
value: data.captcha,
|
||
},
|
||
};
|
||
console.log(payload);
|
||
loginMutation.mutate(payload);
|
||
}
|
||
// if (data) {
|
||
// const base64ImageData = btoa(data);
|
||
// }
|
||
const srcForImage = CaptchaData
|
||
? URL.createObjectURL(CaptchaData.data)
|
||
: null;
|
||
|
||
// const base64ImageData = btoa(data.blob());
|
||
return (
|
||
<div
|
||
className="w-full mt-12 sm:w-2/3 md:w-2/4 xl:w-1/3 bg-white rounded-lg dark:border xl:p-0 dark:bg-gray-800 dark:border-gray-700 shadow-[0px_0px_15px_0px_#00000040]
|
||
h-full max-md:p-8 pt-12 mb-10"
|
||
>
|
||
<div className="space-y-3 md:space-y-4 flex flex-col md:py-12 md:px-20 h-full justify-center">
|
||
<Image alt="icon" src={LoginIcon} className="self-center w-1/3 " />
|
||
|
||
<p className="text-lg font-bold leading-tight tracking-tight text-blue dark:text-white">
|
||
ورود به پنل کاربری
|
||
</p>
|
||
|
||
<Form {...form}>
|
||
<form
|
||
onSubmit={form.handleSubmit(onSubmit)}
|
||
className="w-full flex flex-col "
|
||
>
|
||
<FormField
|
||
control={form.control}
|
||
name="username"
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||
نام کاربری
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input
|
||
className="mb-4"
|
||
placeholder="نام کاربری خود را وارد نمایید"
|
||
{...field}
|
||
/>
|
||
</FormControl>
|
||
{/* <FormDescription>
|
||
This is your public display name.
|
||
</FormDescription> */}
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
|
||
<FormField
|
||
control={form.control}
|
||
name="password"
|
||
render={({ field }) => (
|
||
<FormItem>
|
||
<FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
|
||
رمز عبور
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input placeholder="رمز عبور خود را وارد کنید" {...field} />
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
<div className="flex gap-4 mt-4 items-center">
|
||
<FormField
|
||
control={form.control}
|
||
name="captcha"
|
||
render={({ field }) => (
|
||
<FormItem className=" flex-1 items-center h-10">
|
||
{/* <FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
|
||
رمز عبور
|
||
</FormLabel> */}
|
||
<FormControl>
|
||
<Input placeholder="کد امنیتی" {...field} />
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
{!isLoading && (
|
||
<Image
|
||
alt="icon"
|
||
src={srcForImage as string}
|
||
className=" h-12 rounded-md shadow-md flex-1"
|
||
width={150}
|
||
height={20}
|
||
quality={100}
|
||
/>
|
||
)}
|
||
</div>
|
||
<div className="flex justify-end gap-2 w-full mt-14">
|
||
<Button
|
||
type="submit"
|
||
className="bg-blue text-white w-1/2 md:w-1/3 p-2 rounded-xl"
|
||
>
|
||
ورود
|
||
</Button>
|
||
</div>
|
||
</form>
|
||
</Form>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|