Kahdooei
yasaman 2023-12-28 13:06:32 +03:30
commit f0d4d976ca
4 changed files with 84 additions and 24 deletions

6
app/Verify/page.tsx Normal file
View File

@ -0,0 +1,6 @@
import Verify from "@/components/Verify";
import React from "react";
export default function page() {
return <Verify />;
}

View File

@ -20,8 +20,10 @@ import { Input } from "@/components/ui/input";
import { toast } from "@/components/ui/use-toast"; import { toast } from "@/components/ui/use-toast";
import Image from "next/image"; import Image from "next/image";
import LoginIcon from "../public/img/login-icon.svg"; 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 { loginStaff } from "@/utils/authenticateApi";
import { getCaptcha } from "@/utils/commonApi";
import { useRouter } from "next/navigation";
const FormSchema = z.object({ const FormSchema = z.object({
username: z.string().min(2, { username: z.string().min(2, {
@ -30,10 +32,25 @@ const FormSchema = z.object({
password: z.string().min(4, { password: z.string().min(4, {
message: "Password must be at least 4 characters.", message: "Password must be at least 4 characters.",
}), }),
captcha: z.string().min(4, {
message: "Password must",
}),
}); });
export default function Login() { 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, mutationFn: loginStaff,
onSuccess: (res) => { onSuccess: (res) => {
console.log(res); console.log(res);
@ -45,7 +62,11 @@ export default function Login() {
// router.push("/MainPage"); // router.push("/MainPage");
// } // }
}, },
onError: (err) => {}, onError: (err: any) => {
if (err.response.status == 428) {
router.push("/Verify");
}
},
}); });
const form = useForm<z.infer<typeof FormSchema>>({ const form = useForm<z.infer<typeof FormSchema>>({
@ -65,13 +86,30 @@ export default function Login() {
</pre> </pre>
), ),
}); });
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 ( return (
<div <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] 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" 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"> <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 " /> <Image alt="icon" src={LoginIcon} className="self-center w-1/3 " />
@ -123,7 +161,33 @@ export default function Login() {
</FormItem> </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"> <div className="flex justify-end gap-2 w-full mt-14">
<Button <Button
type="submit" type="submit"

View File

@ -3,22 +3,14 @@ import { CookieValueTypes } from "cookies-next";
axios.defaults.baseURL = "https://sootzaniapi.shetabdahi.ir/"; axios.defaults.baseURL = "https://sootzaniapi.shetabdahi.ir/";
export async function loginStaff({ export async function loginStaff(Data: {
token, username: string;
Data, password: string;
}: { captcha: {
token: CookieValueTypes; key: string;
Data: { value: string;
userName: string;
password: string;
captcha: {
key: string;
value: string;
};
}; };
}) { }) {
const data = await axios.post("api/Authenticate/LoginStaff", Data, { const data = await axios.post("api/Authenticate/LoginStaff", Data);
headers: { Authorization: `Bearer ${token}` },
});
return data.data; return data.data;
} }

View File

@ -1,7 +1,5 @@
import axios from "axios"; import axios from "axios";
import { CookieValueTypes } from "cookies-next"; import { CookieValueTypes } from "cookies-next";
axios.defaults.baseURL = "https://sootzaniapi.shetabdahi.ir/";
// Get Category // Get Category
@ -21,4 +19,4 @@ export async function getCaptcha() {
console.log(data) console.log(data)
return data; return data;
} }