Compare commits

..

2 Commits

Author SHA1 Message Date
Hossein Rahmani 08f9fd2fa5 Merge branch 'master' into Kahdooei 2023-12-25 21:56:46 +03:30
Hossein Rahmani 46075dade5 Part of LoginStaff done 2023-12-25 21:52:45 +03:30
5 changed files with 100 additions and 52 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,7 +20,7 @@ export default function RootLayout({
}) {
return (
<html lang="fa" dir="rtl">
<body className="font-[yekan-m] ">
<body className="font-[yekan] ">
<Provider>
<Navbar />
<NavbarSm />

View File

@ -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<z.infer<typeof FormSchema>>({
@ -65,13 +86,30 @@ export default function Login() {
</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 (
<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"
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 " />
@ -123,7 +161,33 @@ export default function Login() {
</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"

View File

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

View File

@ -3,32 +3,18 @@ import { CookieValueTypes } from "cookies-next";
axios.defaults.baseURL = "https://sootzaniapi.shetabdahi.ir/";
// Get Category
export async function getCategory({token}:{token: CookieValueTypes}) {
const data = await axios.get(
"api/Common/Categories",
{
headers: {
Authorization: `Bearer ${token}`
},
}
);
return data.data;
}
// Get Captcha
export async function getCaptcha({token}:{token: CookieValueTypes}) {
const data = await axios.get(
"api/Common/Captcha",
{
headers: {
Authorization: `Bearer ${token}`
},
}
);
console.log(data)
return data;
}
export async function getCategory({ token }: { token: CookieValueTypes }) {
const data = await axios.get("api/Common/Categories", {
headers: {
Authorization: `Bearer ${token}`,
},
});
return data.data;
}
// Get Captcha
export async function getCaptcha() {
const data = await axios.get("api/Common/Captcha", { responseType: "blob" });
return data;
}