Files
nobat724_front/components/register/verificationPage/SendReq.js
T
hamedandClaude Opus 4.8 08f5829a22 fix(auth): complete login by reading userinfo from response envelope
oauth/userinfo returns { success, data: {...} }, so after the interceptor
unwraps once the user lives at res.data, not res. getInfo checked
res.uuid (undefined), so it never set the userInfo cookie or redirected —
the /login page just sat there after entering the OTP. Read res.data,
store the user object (with a username alias for mobile_number so the
appointment flow keeps working), redirect via location.href, and show a
toast instead of silently staying when userinfo fails.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-15 18:16:36 +03:30

131 lines
3.6 KiB
JavaScript

import { Button, CircularProgress } from "@mui/material";
import { request } from "@/services/response";
import Cookies from "js-cookie";
import { toast } from "react-toastify";
import { handleTimeExpiresToken } from "@/helper";
function SendReq({
loading,
setLoading,
code,
setIsSendMsg,
setStep,
setIsError,
uuid,
}) {
const handleReq = async () => {
setLoading(true);
try {
const res = await fetch("/api/auth/token", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ uuid, code: code.join("") }),
});
const response = await res.json();
if (response.access_token) {
handleSetCookie(response);
} else {
setLoading(false);
setIsError(true);
const message = response?.errors?.[0]?.message || "کد تأیید نادرست یا منقضی شده است.";
toast.error(message);
}
} catch {
setLoading(false);
setIsError(true);
toast.error("خطا در برقراری ارتباط. دوباره تلاش کنید.");
}
};
const handleSetCookie = (response) => {
const expiresTime = handleTimeExpiresToken(response.expires_in);
const hostname = window.location.hostname;
const isHttps = window.location.protocol === "https:";
const isLocalhost = hostname.includes("localhost");
// تنظیمات پایه
let cookieOptions = {
path: "/",
sameSite: "lax",
expires: expiresTime.accessTokenExpires,
};
// اگر روی دامنه اصلی و https بود
if (!isLocalhost && isHttps) {
cookieOptions = {
...cookieOptions,
secure: true,
domain: ".nobat724.com",
};
}
Cookies.set("access_token", response.access_token, cookieOptions);
Cookies.set("refresh_token", response.refresh_token, {
...cookieOptions,
expires: expiresTime.refreshTokenExpires,
});
Cookies.set("uuid", uuid, {
...cookieOptions,
expires: expiresTime.refreshTokenExpires,
});
getInfo(response.access_token, cookieOptions);
};
const getInfo = (token, cookieOptions) => {
request
.getUserInfo({
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((res) => {
setLoading(false);
const profile = res?.data;
if (profile?.uuid) {
const userInfo = { ...profile, username: profile.mobile_number };
Cookies.set("userInfo", JSON.stringify(userInfo), cookieOptions);
// اگر در صفحه appointment هستیم، به مرحله 3 (Detail) می‌رویم
if (setStep) {
setStep(3);
} else {
window.location.href = "/";
}
} else {
setIsError(true);
toast.error("دریافت اطلاعات کاربر ناموفق بود. دوباره تلاش کنید.");
}
})
.catch(() => {
setLoading(false);
setIsError(true);
toast.error("دریافت اطلاعات کاربر ناموفق بود. دوباره تلاش کنید.");
});
};
return (
<Button
fullWidth
disabled={loading}
variant="contained"
onClick={() => {
setIsSendMsg && setIsSendMsg(true);
if (code.join("").length !== 5) {
setIsError(true);
} else {
handleReq();
}
}}
className="!rounded-[8px] !bg-[#5559CE]"
>
{loading ? <CircularProgress size={22} sx={{ color: "#fff" }} /> : "تایید"}
</Button>
);
}
export default SendReq;