Files
nobat724_front/components/register/verificationPage/SendReq.js
T
2025-06-08 10:13:06 +03:30

106 lines
2.3 KiB
JavaScript

import { Button } from "@mui/material";
import { request } from "@/services/response";
import Cookies from "js-cookie";
function SendReq({
loading,
setLoading,
code,
setIsSendMsg,
setStep,
setIsError,
uuid,
}) {
const handleReq = async () => {
setLoading(true);
request
.getToken(
"mobile",
process.env.NEXT_PUBLIC_CLIENT_ID,
process.env.NEXT_PUBLIC_CLIENT_SECRET,
uuid,
code.join("")
)
.then((response) => {
if (response.access_token) {
handleSetCookie(response);
}
})
.catch(() => {});
};
const handleSetCookie = (response) => {
const accessTokenExpires = new Date();
accessTokenExpires.setSeconds(
accessTokenExpires.getSeconds() + response.expires_in
);
const refreshTokenExpires = new Date();
refreshTokenExpires.setDate(refreshTokenExpires.getDate() + 30);
Cookies.set("access_token", response.access_token, {
expires: accessTokenExpires,
path: "/",
secure: true,
sameSite: "strict",
});
Cookies.set("refresh_token", response.refresh_token, {
expires: refreshTokenExpires,
path: "/",
secure: true,
sameSite: "strict",
});
Cookies.set("uuid", uuid, {
expires: refreshTokenExpires,
path: "/",
secure: true,
sameSite: "strict",
});
getInfo(response.access_token);
};
const getInfo = (token) => {
request
.getUserInfo({
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((res) => {
setLoading(false);
if (res && res.uuid) {
window.location.pathname = "/";
Cookies.set("userInfo", JSON.stringify(res), {
path: "/",
secure: true,
sameSite: "strict",
});
}
})
.catch(() => {});
};
return (
<Button
fullWidth
loading={loading}
variant="contained"
onClick={() => {
setIsSendMsg && setIsSendMsg(true);
setStep && setStep((prev) => prev + 1);
if (code.join("").length !== 5) {
setIsError(true);
} else {
handleReq();
}
}}
className="!rounded-[8px] !bg-[#5559CE]"
>
تایید
</Button>
);
}
export default SendReq;