diff --git a/app/Verify/page.tsx b/app/Verify/page.tsx new file mode 100644 index 0000000..e2a366b --- /dev/null +++ b/app/Verify/page.tsx @@ -0,0 +1,6 @@ +import Verify from "@/components/Verify"; +import React from "react"; + +export default function page() { + return ; +} diff --git a/app/layout.tsx b/app/layout.tsx index 2fc2253..7454629 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -20,16 +20,16 @@ export default function RootLayout({ }) { return ( - - + + {children} - - + + ); } diff --git a/components/Login.tsx b/components/Login.tsx index 8fa78ec..df2b2f6 100644 --- a/components/Login.tsx +++ b/components/Login.tsx @@ -20,8 +20,10 @@ 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 } from "@tanstack/react-query"; +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, { @@ -30,10 +32,25 @@ const FormSchema = z.object({ 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 LoginMutation = useMutation({ + 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); @@ -45,7 +62,11 @@ export default function Login() { // router.push("/MainPage"); // } }, - onError: (err) => {}, + onError: (err: any) => { + if (err.response.status == 428) { + router.push("/Verify"); + } + }, }); const form = useForm>({ @@ -65,13 +86,30 @@ export default function Login() { ), }); - console.log(data); + 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 ( @@ -123,7 +161,33 @@ export default function Login() { )} /> - + + ( + + {/* + رمز عبور + */} + + + + + + )} + /> + {!isLoading && ( + + )} +